Skip to content

Commit 20359cc

Browse files
committed
CORS-3218: GCP Remove the use case of users passing contents for json secret data.
**Expose environment variable strings for GCP credentials. **CAPG infrastructure controller will look for the gcp credentials to determine which contains a file location. This will be supplied to the controller. **The default file location for capg infrastructure controller will remain the same, but it will use the default location from the gcp credentials file in the install config package.
1 parent ac3ac89 commit 20359cc

File tree

2 files changed

+67
-19
lines changed

2 files changed

+67
-19
lines changed

pkg/asset/installconfig/gcp/session.go

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ var (
2525
// Session is an object representing session for GCP API.
2626
type Session struct {
2727
Credentials *googleoauth.Credentials
28+
29+
// Path contains the filepath for provided credentials. When authenticating with
30+
// Default Application Credentials, Path will be empty.
31+
Path string
2832
}
2933

3034
// GetSession returns a GCP session by using credentials found in default locations in order:
@@ -35,17 +39,18 @@ type Session struct {
3539
// gcloud cli defaults
3640
// and, if no creds are found, asks for them and stores them on disk in a config file
3741
func GetSession(ctx context.Context) (*Session, error) {
38-
creds, err := loadCredentials(ctx)
42+
creds, path, err := loadCredentials(ctx)
3943
if err != nil {
4044
return nil, errors.Wrap(err, "failed to load credentials")
4145
}
4246

4347
return &Session{
4448
Credentials: creds,
49+
Path: path,
4550
}, nil
4651
}
4752

48-
func loadCredentials(ctx context.Context) (*googleoauth.Credentials, error) {
53+
func loadCredentials(ctx context.Context) (*googleoauth.Credentials, string, error) {
4954
if len(credLoaders) == 0 {
5055
for _, authEnv := range authEnvs {
5156
credLoaders = append(credLoaders, &envLoader{env: authEnv})
@@ -66,30 +71,31 @@ func loadCredentials(ctx context.Context) (*googleoauth.Credentials, error) {
6671
onceLoggers[loader].Do(func() {
6772
logrus.Infof("Credentials loaded from %s", loader)
6873
})
69-
return creds, nil
74+
return creds, loader.Content(), nil
7075
}
7176
return getCredentials(ctx)
7277
}
7378

74-
func getCredentials(ctx context.Context) (*googleoauth.Credentials, error) {
79+
func getCredentials(ctx context.Context) (*googleoauth.Credentials, string, error) {
7580
creds, err := (&userLoader{}).Load(ctx)
7681
if err != nil {
77-
return nil, err
82+
return nil, "", err
7883
}
7984

8085
filePath := defaultAuthFilePath
8186
logrus.Infof("Saving the credentials to %q", filePath)
8287
if err := os.MkdirAll(filepath.Dir(filePath), 0700); err != nil {
83-
return nil, err
88+
return nil, "", err
8489
}
8590
if err := os.WriteFile(filePath, creds.JSON, 0o600); err != nil {
86-
return nil, err
91+
return nil, "", err
8792
}
88-
return creds, nil
93+
return creds, filePath, nil
8994
}
9095

9196
type credLoader interface {
9297
Load(context.Context) (*googleoauth.Credentials, error)
98+
Content() string
9399
}
94100

95101
type envLoader struct {
@@ -115,19 +121,25 @@ func (e *envLoader) String() string {
115121
return strings.Join(path, ", ")
116122
}
117123

124+
func (e *envLoader) Content() string {
125+
envValue, found := os.LookupEnv(e.env)
126+
if !found {
127+
return ""
128+
}
129+
return envValue
130+
}
131+
118132
type fileOrContentLoader struct {
119133
pathOrContent string
120134
delegate credLoader
121135
}
122136

123137
func (fc *fileOrContentLoader) Load(ctx context.Context) (*googleoauth.Credentials, error) {
124138
// if this is a path and we can stat it, assume it's ok
125-
if _, err := os.Stat(fc.pathOrContent); err == nil {
126-
fc.delegate = &fileLoader{path: fc.pathOrContent}
127-
} else {
128-
fc.delegate = &contentLoader{content: fc.pathOrContent}
139+
if _, err := os.Stat(fc.pathOrContent); err != nil {
140+
return nil, fmt.Errorf("supplied value should be the path to a GCP credentials file: %w", err)
129141
}
130-
142+
fc.delegate = &fileLoader{path: fc.pathOrContent}
131143
return fc.delegate.Load(ctx)
132144
}
133145

@@ -138,6 +150,13 @@ func (fc *fileOrContentLoader) String() string {
138150
return "file or content"
139151
}
140152

153+
func (fc *fileOrContentLoader) Content() string {
154+
if _, err := os.Stat(fc.pathOrContent); err != nil {
155+
return ""
156+
}
157+
return fc.pathOrContent
158+
}
159+
141160
type fileLoader struct {
142161
path string
143162
}
@@ -154,6 +173,10 @@ func (f *fileLoader) String() string {
154173
return fmt.Sprintf("file %q", f.path)
155174
}
156175

176+
func (f *fileLoader) Content() string {
177+
return f.path
178+
}
179+
157180
type contentLoader struct {
158181
content string
159182
}
@@ -166,6 +189,10 @@ func (f *contentLoader) String() string {
166189
return "content <redacted>"
167190
}
168191

192+
func (f *contentLoader) Content() string {
193+
return ""
194+
}
195+
169196
type cliLoader struct{}
170197

171198
func (c *cliLoader) Load(ctx context.Context) (*googleoauth.Credentials, error) {
@@ -176,14 +203,18 @@ func (c *cliLoader) String() string {
176203
return "gcloud CLI defaults"
177204
}
178205

206+
func (c *cliLoader) Content() string {
207+
return ""
208+
}
209+
179210
type userLoader struct{}
180211

181212
func (u *userLoader) Load(ctx context.Context) (*googleoauth.Credentials, error) {
182213
var content string
183214
err := survey.Ask([]*survey.Question{
184215
{
185216
Prompt: &survey.Multiline{
186-
Message: "Service Account (absolute path to file or JSON content)",
217+
Message: "Service Account (absolute path to file)",
187218
// Due to a bug in survey pkg, help message is not rendered
188219
Help: "The location to file that contains the service account in JSON, or the service account in JSON format",
189220
},
@@ -193,5 +224,9 @@ func (u *userLoader) Load(ctx context.Context) (*googleoauth.Credentials, error)
193224
return nil, err
194225
}
195226
content = strings.TrimSpace(content)
196-
return (&fileOrContentLoader{pathOrContent: content}).Load(ctx)
227+
return (&fileLoader{path: content}).Load(ctx)
228+
}
229+
230+
func (u *userLoader) Content() string {
231+
return defaultAuthFilePath
197232
}

pkg/clusterapi/system.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/openshift/installer/data"
2020
"github.com/openshift/installer/pkg/asset/installconfig"
21+
gcpic "github.com/openshift/installer/pkg/asset/installconfig/gcp"
2122
powervsic "github.com/openshift/installer/pkg/asset/installconfig/powervs"
2223
"github.com/openshift/installer/pkg/clusterapi/internal/process"
2324
"github.com/openshift/installer/pkg/clusterapi/internal/process/addr"
@@ -175,6 +176,21 @@ func (c *system) Run(ctx context.Context, installConfig *installconfig.InstallCo
175176
),
176177
)
177178
case gcp.Name:
179+
session, err := gcpic.GetSession(context.Background())
180+
if err != nil {
181+
return fmt.Errorf("failed to create gcp session: %w", err)
182+
}
183+
184+
//nolint:gosec // CAPG only expects a single credentials environment variable
185+
gAppCredEnvVar := "GOOGLE_APPLICATION_CREDENTIALS"
186+
capgEnvVars := map[string]string{
187+
gAppCredEnvVar: session.Path,
188+
}
189+
190+
if v, ok := capgEnvVars[gAppCredEnvVar]; ok {
191+
logrus.Infof("setting %q to %s for capg infrastructure controller", gAppCredEnvVar, v)
192+
}
193+
178194
controllers = append(controllers,
179195
c.getInfrastructureController(
180196
&GCP,
@@ -185,10 +201,7 @@ func (c *system) Run(ctx context.Context, installConfig *installconfig.InstallCo
185201
"--webhook-port={{.WebhookPort}}",
186202
"--webhook-cert-dir={{.WebhookCertDir}}",
187203
},
188-
map[string]string{
189-
// TODO: Authentication must be handled in a more complex way detailed here: https://issues.redhat.com/browse/CORS-3218
190-
"GOOGLE_APPLICATION_CREDENTIALS": filepath.Join(os.Getenv("HOME"), ".gcp", "osServiceAccount.json"),
191-
},
204+
capgEnvVars,
192205
),
193206
)
194207
case ibmcloud.Name:

0 commit comments

Comments
 (0)