Skip to content

Commit

Permalink
[CLOUDTRUST-2109] Load authorization from DB
Browse files Browse the repository at this point in the history
  • Loading branch information
harture authored Feb 19, 2020
1 parent 790d30f commit 99fca6e
Show file tree
Hide file tree
Showing 5 changed files with 511 additions and 243 deletions.
58 changes: 17 additions & 41 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions configuration/dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package configuration

// RealmConfiguration struct
type RealmConfiguration struct {
DefaultClientID *string `json:"default_client_id,omitempty"`
DefaultRedirectURI *string `json:"default_redirect_uri,omitempty"`
APISelfAuthenticatorDeletionEnabled *bool `json:"api_self_authenticator_deletion_enabled,omitempty"`
APISelfPasswordChangeEnabled *bool `json:"api_self_password_change_enabled,omitempty"`
APISelfMailEditingEnabled *bool `json:"api_self_mail_editing_enabled,omitempty"`
APISelfAccountDeletionEnabled *bool `json:"api_self_account_deletion_enabled,omitempty"`
ShowAuthenticatorsTab *bool `json:"show_authenticators_tab,omitempty"`
ShowPasswordTab *bool `json:"show_password_tab,omitempty"`
ShowMailEditing *bool `json:"show_mail_editing,omitempty"`
ShowAccountDeletionButton *bool `json:"show_account_deletion_button,omitempty"`
RegisterExecuteActions *[]string `json:"register_execute_actions,omitempty"`
RedirectCancelledRegistrationURL *string `json:"redirect_cancelled_registration_url,omitempty"`
RedirectSuccessfulRegistrationURL *string `json:"redirect_successful_registration_url,omitempty"`
}

// Authorization struct
type Authorization struct {
RealmID *string `json:"realm_id"`
GroupName *string `json:"group_id"`
Action *string `json:"action"`
TargetRealmID *string `json:"target_realm_id,omitempty"`
TargetGroupName *string `json:"target_group_name,omitempty"`
}
102 changes: 102 additions & 0 deletions configuration/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package configuration

import (
"context"
"database/sql"
"encoding/json"

"github.com/cloudtrust/common-service/database/sqltypes"
"github.com/cloudtrust/common-service/log"
)

const (
selectConfigStmt = `SELECT configuration FROM realm_configuration WHERE (realm_id = ?)`
selectAllAuthzStmt = `SELECT realm_id, group_name, action, target_realm_id, target_group_name FROM authorizations;`
)

type ConfigurationReaderDBModule struct {
db sqltypes.CloudtrustDB
logger log.Logger
}

// NewConfigurationDBModule returns a ConfigurationDB module.
func NewConfigurationReaderDBModule(db sqltypes.CloudtrustDB, logger log.Logger) *ConfigurationReaderDBModule {
return &ConfigurationReaderDBModule{
db: db,
logger: logger,
}
}

func (c *ConfigurationReaderDBModule) GetConfiguration(ctx context.Context, realmID string) (RealmConfiguration, error) {
var configJSON string
var config = RealmConfiguration{}
row := c.db.QueryRow(selectConfigStmt, realmID)

switch err := row.Scan(&configJSON); err {
case sql.ErrNoRows:
c.logger.Warn(ctx, "msg", "Realm Configuration not found in DB", "error", err.Error())
return config, err

default:
if err != nil {
return config, err
}

err = json.Unmarshal([]byte(configJSON), &config)
return config, err
}
}

func (c *ConfigurationReaderDBModule) GetAuthorizations(ctx context.Context) ([]Authorization, error) {
// Get Authorizations from DB
rows, err := c.db.Query(selectAllAuthzStmt)
if err != nil {
c.logger.Warn(ctx, "msg", "Can't get authorizations", "error", err.Error())
return nil, err
}
defer rows.Close()

var authz Authorization
var res = make([]Authorization, 0)
for rows.Next() {
authz, err = c.scanAuthorization(rows)
if err != nil {
c.logger.Warn(ctx, "msg", "Can't get authorizations. Scan failed", "error", err.Error())
return nil, err
}
res = append(res, authz)
}

return res, nil
}

func (c *ConfigurationReaderDBModule) scanAuthorization(scanner sqltypes.SQLRow) (Authorization, error) {
var (
realmID string
groupName string
action string
targetGroupName sql.NullString
targetRealmID sql.NullString
)

err := scanner.Scan(&realmID, &groupName, &action, &targetRealmID, &targetGroupName)
if err != nil {
return Authorization{}, err
}

var authz = Authorization{
RealmID: &realmID,
GroupName: &groupName,
Action: &action,
}

if targetRealmID.Valid {
authz.TargetRealmID = &targetRealmID.String
}

if targetGroupName.Valid {
authz.TargetGroupName = &targetGroupName.String
}

return authz, nil
}
Loading

0 comments on commit 99fca6e

Please sign in to comment.