Skip to content

Commit

Permalink
Refactor orgid and user auth and move them into new structs
Browse files Browse the repository at this point in the history
Signed-off-by: Burak Sekili <buraksekili@gmail.com>
  • Loading branch information
buraksekili committed Nov 3, 2023
1 parent 2590ee7 commit 5283493
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
15 changes: 9 additions & 6 deletions data/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,22 @@ type TykAdmin struct {
EmailAddress string
// Password corresponds to the password of the admin being created.
Password string

// Auth corresponds to Tyk Dashboard API Access Credentials of the admin user, and it will be used
// in Authorization header of the HTTP requests that will be sent to Tyk for bootstrapping.
// Also, if bootstrapping Tyk Operator Secret is enabled Auth corresponds to TykAuth field in the
// Kubernetes secret of Tyk Operator.
Auth string `ignored:"true"`
}

type TykOrg struct {
// Name corresponds to the name for your organization that is going to be bootstrapped in Tyk
Name string
// Cname corresponds to the Organisation CNAME which is going to bind the Portal to.
Cname string

// ID corresponds to the organisation ID that is being created.
ID string `ignored:"true"`
}

type TykConf struct {
Expand All @@ -79,12 +88,6 @@ type TykConf struct {
Org TykOrg

DashboardLicense string

// UserAuth corresponds to AuthCode of the created user, and it will be used in Authorization header
// of the HTTP requests that will be sent to Tyk for bootstrapping. Also, if bootstrapping Operator Secret
// is enabled via TODO: add here, UserAuth corresponds to TykAuth field in the Kubernetes secret for Tyk Operator.
UserAuth string `ignored:"true"`
OrgId string `ignored:"true"`
}

var BootstrapConf = Config{}
Expand Down
8 changes: 4 additions & 4 deletions helpers/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func BootstrapTykOperatorSecret() error {

func CreateTykOperatorSecret(clientset *kubernetes.Clientset) error {
secretData := map[string][]byte{
TykAuth: []byte(data.BootstrapConf.Tyk.UserAuth),
TykOrg: []byte(data.BootstrapConf.Tyk.OrgId),
TykAuth: []byte(data.BootstrapConf.Tyk.Admin.Auth),
TykOrg: []byte(data.BootstrapConf.Tyk.Org.ID),
TykMode: []byte(TykModePro),
TykUrl: []byte(data.BootstrapConf.K8s.DashboardSvcUrl),
}
Expand Down Expand Up @@ -127,8 +127,8 @@ func BootstrapTykPortalSecret() error {

func CreateTykPortalSecret(clientset *kubernetes.Clientset, secretName string) error {
secretData := map[string][]byte{
TykAuth: []byte(data.BootstrapConf.Tyk.UserAuth),
TykOrg: []byte(data.BootstrapConf.Tyk.OrgId),
TykAuth: []byte(data.BootstrapConf.Tyk.Admin.Auth),
TykOrg: []byte(data.BootstrapConf.Tyk.Org.ID),
}

objectMeta := metav1.ObjectMeta{Name: secretName}
Expand Down
10 changes: 5 additions & 5 deletions helpers/portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func SetPortalCname(client http.Client) error {
return err
}

req.Header.Set(data.AuthorizationHeader, data.BootstrapConf.Tyk.UserAuth)
req.Header.Set(data.AuthorizationHeader, data.BootstrapConf.Tyk.Admin.Auth)

res, err := client.Do(req)
if err != nil {
Expand All @@ -87,7 +87,7 @@ func SetPortalCname(client http.Client) error {
func InitialiseCatalogue(client http.Client) error {
fmt.Println("Initialising Catalogue")

initCatalog := InitCatalogReq{OrgId: data.BootstrapConf.Tyk.OrgId}
initCatalog := InitCatalogReq{OrgId: data.BootstrapConf.Tyk.Org.ID}

reqBody, err := json.Marshal(initCatalog)
if err != nil {
Expand All @@ -103,7 +103,7 @@ func InitialiseCatalogue(client http.Client) error {
return err
}

req.Header.Set(data.AuthorizationHeader, data.BootstrapConf.Tyk.UserAuth)
req.Header.Set(data.AuthorizationHeader, data.BootstrapConf.Tyk.Admin.Auth)

res, err := client.Do(req)
if err != nil || res.StatusCode != http.StatusOK {
Expand Down Expand Up @@ -142,7 +142,7 @@ func CreatePortalHomepage(client http.Client) error {
return err
}

req.Header.Set(data.AuthorizationHeader, data.BootstrapConf.Tyk.UserAuth)
req.Header.Set(data.AuthorizationHeader, data.BootstrapConf.Tyk.Admin.Auth)

res, err := client.Do(req)
if err != nil || res.StatusCode != http.StatusOK {
Expand Down Expand Up @@ -231,7 +231,7 @@ func CreatePortalDefaultSettings(client http.Client) error {
data.BootstrapConf.K8s.DashboardSvcUrl+ApiPortalConfigurationEndpoint,
nil,
)
req.Header.Set(data.AuthorizationHeader, data.BootstrapConf.Tyk.UserAuth)
req.Header.Set(data.AuthorizationHeader, data.BootstrapConf.Tyk.Admin.Auth)

if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions helpers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ func GenerateDashboardCredentials(client http.Client) error {
return err
}

data.BootstrapConf.Tyk.OrgId = orgId
data.BootstrapConf.Tyk.Org.ID = orgId

userAuth, err := CreateUser(client, data.BootstrapConf.K8s.DashboardSvcUrl, orgId)
if err != nil {
return err
}

data.BootstrapConf.Tyk.UserAuth = userAuth
data.BootstrapConf.Tyk.Admin.Auth = userAuth

return nil
}
Expand Down

0 comments on commit 5283493

Please sign in to comment.