@@ -18,7 +18,6 @@ package config
18
18
19
19
import (
20
20
"encoding/json"
21
- "os"
22
21
"strings"
23
22
24
23
"github.com/IBM/secret-utils-lib/pkg/k8s_utils"
@@ -41,8 +40,9 @@ const (
41
40
42
41
// ClusterConfig ...
43
42
type ClusterConfig struct {
44
- ClusterID string `json:"cluster_id"`
45
- MasterURL string `json:"master_url"`
43
+ ClusterID string `json:"cluster_id"`
44
+ MasterURL string `json:"master_url"`
45
+ ClusterProvider string `json:"cluster_provider"`
46
46
}
47
47
48
48
// GetClusterInfo ...
@@ -72,29 +72,31 @@ func FrameTokenExchangeURL(kc k8s_utils.KubernetesClient, providerType string, l
72
72
return cloudConf .TokenExchangeURL + tokenExchangePath
73
73
}
74
74
75
+ cc , err := GetClusterInfo (kc , logger )
76
+ if err != nil {
77
+ logger .Error ("Error fetching cluster info" , zap .Error (err ))
78
+ return (utils .ProdPrivateIAMURL + tokenExchangePath )
79
+ }
80
+
81
+ isSatellite := IsSatellite (cc , logger )
82
+
75
83
logger .Info ("Unable to fetch token exchange URL from cloud-conf" )
76
84
secret , err := k8s_utils .GetSecretData (kc , utils .STORAGE_SECRET_STORE_SECRET , utils .SECRET_STORE_FILE )
77
85
if err == nil {
78
86
if secretConfig , err := ParseConfig (logger , secret ); err == nil {
79
- url , err := GetTokenExchangeURLfromStorageSecretStore (* secretConfig , providerType )
87
+ url , err := GetTokenExchangeURLfromStorageSecretStore (isSatellite , * secretConfig , providerType )
80
88
if err == nil {
81
89
return url
82
90
}
83
91
}
84
92
}
85
93
86
94
logger .Info ("Unable to fetch token exchange URL using secret, forming url using cluster info" )
87
- cc , err := GetClusterInfo (kc , logger )
88
- if err != nil {
89
- logger .Error ("Error fetching cluster master URL" , zap .Error (err ))
90
- return (utils .ProdPublicIAMURL + tokenExchangePath )
91
- }
92
-
93
- return FrameTokenExchangeURLFromClusterInfo (cc , logger )
95
+ return FrameTokenExchangeURLFromClusterInfo (isSatellite , cc , logger )
94
96
}
95
97
96
98
// GetTokenExchangeURLfromStorageSecretStore ...
97
- func GetTokenExchangeURLfromStorageSecretStore (config Config , providerType string ) (string , error ) {
99
+ func GetTokenExchangeURLfromStorageSecretStore (isSatellite bool , config Config , providerType string ) (string , error ) {
98
100
99
101
var url string
100
102
switch providerType {
@@ -110,28 +112,49 @@ func GetTokenExchangeURLfromStorageSecretStore(config Config, providerType strin
110
112
return "" , utils.Error {Description : utils .WarnFetchingTokenExchangeURL }
111
113
}
112
114
113
- // If the cluster is not satellite cluster, use PROD or STAGE URLs
114
- if os .Getenv ("IS_SATELLITE" ) != constTrue {
115
- if ! strings .Contains (url , "stage" ) && ! strings .Contains (url , "test" ) {
116
- url = utils .ProdPrivateIAMURL
117
- } else {
118
- url = utils .StagePrivateIAMURL
119
- }
115
+ // If the cluster is satellite, first use the provided URL.
116
+ if isSatellite {
117
+ return url , nil
120
118
}
121
119
122
- // Appending the base URL and token exchange path
123
- url = url + tokenExchangePath
124
-
125
- return url , nil
120
+ isProd := isProduction (url )
121
+ if isProd {
122
+ return utils .ProdPrivateIAMURL + tokenExchangePath , nil
123
+ }
124
+ return utils .StagePrivateIAMURL + tokenExchangePath , nil
126
125
}
127
126
128
127
// FrameTokenExchangeURLFromClusterInfo ...
129
- func FrameTokenExchangeURLFromClusterInfo (cc ClusterConfig , logger * zap.Logger ) string {
128
+ func FrameTokenExchangeURLFromClusterInfo (isSatellite bool , cc ClusterConfig , logger * zap.Logger ) string {
129
+
130
130
if ! strings .Contains (cc .MasterURL , stageMasterURLsubstr ) {
131
131
logger .Info ("Env-Production" )
132
+ if isSatellite {
133
+ return (utils .ProdPublicIAMURL + tokenExchangePath )
134
+ }
132
135
return (utils .ProdPrivateIAMURL + tokenExchangePath )
133
136
}
134
137
135
138
logger .Info ("Env-Stage" )
139
+ if isSatellite {
140
+ return (utils .StagePublicIAMURL + tokenExchangePath )
141
+ }
136
142
return (utils .StagePrivateIAMURL + tokenExchangePath )
137
143
}
144
+
145
+ // isProduction determines if the env in which a pod is deployed is stage or production
146
+ func isProduction (url string ) bool {
147
+ if ! strings .Contains (url , "stage" ) && ! strings .Contains (url , "test" ) {
148
+ return true
149
+ }
150
+ return false
151
+ }
152
+
153
+ // IsSatellite checks if the cluster where the pod is currently running is a satellite cluster or not
154
+ func IsSatellite (cc ClusterConfig , logger * zap.Logger ) bool {
155
+ if cc .ClusterProvider == utils .SatelliteProvider {
156
+ return true
157
+ }
158
+
159
+ return false
160
+ }
0 commit comments