From 87e79110a23c2c67aff4b68b17deda3639a1190a Mon Sep 17 00:00:00 2001 From: dwertent Date: Tue, 19 Oct 2021 16:25:54 +0300 Subject: [PATCH] fallback customer guid from configMap --- cautils/customerloader.go | 92 +++++++++++++++++++++++++++------------ cmd/framework.go | 4 +- 2 files changed, 65 insertions(+), 31 deletions(-) diff --git a/cautils/customerloader.go b/cautils/customerloader.go index c97acdb1..8e723b20 100644 --- a/cautils/customerloader.go +++ b/cautils/customerloader.go @@ -30,6 +30,7 @@ type ConfigObj struct { CustomerGUID string `json:"customerGUID"` Token string `json:"invitationParam"` CustomerAdminEMail string `json:"adminMail"` + ClusterName string `json:"clusterName"` } func (co *ConfigObj) Json() []byte { @@ -39,14 +40,30 @@ func (co *ConfigObj) Json() []byte { return []byte{} } +// Config - convert ConfigObj to config file +func (co *ConfigObj) Config() []byte { + clusterName := co.ClusterName + co.ClusterName = "" // remove cluster name before saving to file + b, err := json.Marshal(co) + co.ClusterName = clusterName + + if err == nil { + return b + } + + return []byte{} +} + // ====================================================================================== // =============================== interface ============================================ // ====================================================================================== type IClusterConfig interface { - // setters - SetCustomerGUID(customerGUID string) error + + // set + SetConfig(customerGUID string) error // getters + GetClusterName() string GetCustomerGUID() string GetConfigObj() *ConfigObj GetK8sAPI() *k8sinterface.KubernetesApi @@ -103,13 +120,14 @@ func ClusterConfigSetup(scanInfo *ScanInfo, k8s *k8sinterface.KubernetesApi, beA type EmptyConfig struct { } -func NewEmptyConfig() *EmptyConfig { return &EmptyConfig{} } -func (c *EmptyConfig) GetConfigObj() *ConfigObj { return &ConfigObj{} } -func (c *EmptyConfig) SetCustomerGUID(customerGUID string) error { return nil } -func (c *EmptyConfig) GetCustomerGUID() string { return "" } -func (c *EmptyConfig) GetK8sAPI() *k8sinterface.KubernetesApi { return nil } // TODO: return mock obj -func (c *EmptyConfig) GetDefaultNS() string { return k8sinterface.GetDefaultNamespace() } -func (c *EmptyConfig) GetBackendAPI() getter.IBackend { return nil } // TODO: return mock obj +func NewEmptyConfig() *EmptyConfig { return &EmptyConfig{} } +func (c *EmptyConfig) SetConfig(customerGUID string) error { return nil } +func (c *EmptyConfig) GetConfigObj() *ConfigObj { return &ConfigObj{} } +func (c *EmptyConfig) GetCustomerGUID() string { return "" } +func (c *EmptyConfig) GetK8sAPI() *k8sinterface.KubernetesApi { return nil } // TODO: return mock obj +func (c *EmptyConfig) GetDefaultNS() string { return k8sinterface.GetDefaultNamespace() } +func (c *EmptyConfig) GetBackendAPI() getter.IBackend { return nil } // TODO: return mock obj +func (c *EmptyConfig) GetClusterName() string { return "unknown" } func (c *EmptyConfig) GenerateURL() { message := fmt.Sprintf("You can see the results in a user-friendly UI, choose your preferred compliance framework, check risk results history and trends, manage exceptions, get remediation recommendations and much more by registering here: https://%s", getter.GetArmoAPIConnector().GetFrontendURL()) InfoTextDisplay(os.Stdout, message+"\n") @@ -169,10 +187,19 @@ func (c *ClusterConfig) GetCustomerGUID() string { return "" } -func (c *ClusterConfig) SetCustomerGUID(customerGUID string) error { +func (c *ClusterConfig) SetConfig(customerGUID string) error { + if c.configObj == nil { + c.configObj = &ConfigObj{} + } + // cluster name + if c.GetClusterName() == "" { + c.setClusterName(k8sinterface.GetClusterName()) + } + + // ARMO customer GUID if customerGUID != "" && c.GetCustomerGUID() != customerGUID { - c.configObj.CustomerGUID = customerGUID // override config customerGUID + c.setCustomerGUID(customerGUID) // override config customerGUID } customerGUID = c.GetCustomerGUID() @@ -181,10 +208,10 @@ func (c *ClusterConfig) SetCustomerGUID(customerGUID string) error { tenantResponse, err := c.backendAPI.GetCustomerGUID(customerGUID) if err == nil && tenantResponse != nil { if tenantResponse.AdminMail != "" { // this customer already belongs to some user - c.configObj.CustomerAdminEMail = tenantResponse.AdminMail + c.setCustomerAdminEMail(tenantResponse.AdminMail) } else { - c.configObj.Token = tenantResponse.Token - c.configObj.CustomerGUID = tenantResponse.TenantID + c.setToken(tenantResponse.Token) + c.setCustomerGUID(tenantResponse.TenantID) } } else { if err != nil && !strings.Contains(err.Error(), "already exists") { @@ -198,15 +225,28 @@ func (c *ClusterConfig) SetCustomerGUID(customerGUID string) error { } else { c.createConfigMap() } - if existsConfigFile() { - c.updateConfigFile() - } else { - c.createConfigFile() - } + c.updateConfigFile() return nil } +func (c *ClusterConfig) setToken(token string) { + c.configObj.Token = token +} + +func (c *ClusterConfig) setCustomerAdminEMail(customerAdminEMail string) { + c.configObj.CustomerAdminEMail = customerAdminEMail +} +func (c *ClusterConfig) setCustomerGUID(customerGUID string) { + c.configObj.CustomerGUID = customerGUID +} + +func (c *ClusterConfig) setClusterName(clusterName string) { + c.configObj.ClusterName = clusterName +} +func (c *ClusterConfig) GetClusterName() string { + return c.configObj.ClusterName +} func (c *ClusterConfig) LoadConfig() { // get from configMap if c.existsConfigMap() { @@ -220,8 +260,9 @@ func (c *ClusterConfig) LoadConfig() { func (c *ClusterConfig) ToMapString() map[string]interface{} { m := map[string]interface{}{} - bc, _ := json.Marshal(c.configObj) - json.Unmarshal(bc, &m) + if bc, err := json.Marshal(c.configObj); err == nil { + json.Unmarshal(bc, &m) + } return m } func (c *ClusterConfig) loadConfigFromConfigMap() (*ConfigObj, error) { @@ -360,14 +401,7 @@ func (c *ClusterConfig) updateConfigMap() error { } func (c *ClusterConfig) updateConfigFile() error { - if err := os.WriteFile(ConfigFileFullPath(), c.configObj.Json(), 0664); err != nil { - return err - } - return nil -} - -func (c *ClusterConfig) createConfigFile() error { - if err := os.WriteFile(ConfigFileFullPath(), c.configObj.Json(), 0664); err != nil { + if err := os.WriteFile(ConfigFileFullPath(), c.configObj.Config(), 0664); err != nil { return err } return nil diff --git a/cmd/framework.go b/cmd/framework.go index c24a4fdf..954ef634 100644 --- a/cmd/framework.go +++ b/cmd/framework.go @@ -120,12 +120,12 @@ func CliSetup() error { // policy handler setup policyHandler := policyhandler.NewPolicyHandler(&processNotification, k8s) - if err := clusterConfig.SetCustomerGUID(scanInfo.Account); err != nil { + if err := clusterConfig.SetConfig(scanInfo.Account); err != nil { fmt.Println(err) } + cautils.ClusterName = clusterConfig.GetClusterName() cautils.CustomerGUID = clusterConfig.GetCustomerGUID() - cautils.ClusterName = k8sinterface.GetClusterName() // cli handler setup go func() {