Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not need Google credentials before using it #21072

Merged
merged 2 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
*Functionbeat*

- Fix timeout option of GCP functions. {issue}16282[16282] {pull}16287[16287]
- Do not need Google credentials if not required for the operation. {issue}17329[17329] {pull}21072[21072]

==== Added

Expand Down
38 changes: 28 additions & 10 deletions x-pack/functionbeat/manager/gcp/cli_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,19 @@ func (c *CLIManager) deploy(update bool, name string) error {
executer.Add(newOpEnsureBucket(c.log, c.config))
executer.Add(newOpUploadToBucket(c.log, c.config, name, functionData.raw))

token, err := c.getTokenSrc()
if err != nil {
return err
}

ctx := &functionContext{}
if update {
executer.Add(newOpUpdateFunction(ctx, c.log, c.tokenSrc, functionData.function.Name, functionData.function))
executer.Add(newOpUpdateFunction(ctx, c.log, token, functionData.function.Name, functionData.function))
} else {
executer.Add(newOpCreateFunction(ctx, c.log, c.tokenSrc, c.location, name, functionData.function))
executer.Add(newOpCreateFunction(ctx, c.log, token, c.location, name, functionData.function))
}

executer.Add(newOpWaitForFunction(ctx, c.log, c.tokenSrc))
executer.Add(newOpWaitForFunction(ctx, c.log, token))

if err := executer.Execute(nil); err != nil {
if rollbackErr := executer.Rollback(nil); rollbackErr != nil {
Expand All @@ -104,9 +109,14 @@ func (c *CLIManager) Remove(name string) error {
return err
}

token, err := c.getTokenSrc()
if err != nil {
return err
}

ctx := &functionContext{}
executer := executor.NewExecutor(c.log)
executer.Add(newOpDeleteFunction(ctx, c.log, c.location, functionData.function.Name, c.tokenSrc))
executer.Add(newOpDeleteFunction(ctx, c.log, c.location, functionData.function.Name, token))
executer.Add(newOpDeleteFromBucket(c.log, c.config, name))

if err := executer.Execute(nil); err != nil {
Expand Down Expand Up @@ -151,6 +161,20 @@ func (c *CLIManager) Package(outputPattern string) error {
return nil
}

func (c *CLIManager) getTokenSrc() (oauth2.TokenSource, error) {
if c.tokenSrc != nil {
return c.tokenSrc, nil
}

var err error
c.tokenSrc, err = google.DefaultTokenSource(context.Background(), "https://www.googleapis.com/auth/cloud-platform")
if err != nil {
return nil, fmt.Errorf("error while creating CLIManager: %+v", err)
}

return c.tokenSrc, nil
}

// NewCLI returns the interface to manage functions on Google Cloud Platform.
func NewCLI(
log *logp.Logger,
Expand All @@ -173,16 +197,10 @@ func NewCLI(

location := fmt.Sprintf(locationTemplate, config.ProjectID, config.Location)

tokenSrc, err := google.DefaultTokenSource(context.TODO(), "https://www.googleapis.com/auth/cloud-platform")
if err != nil {
return nil, fmt.Errorf("error while creating CLIManager: %+v", err)
}

return &CLIManager{
config: config,
log: logp.NewLogger("gcp"),
location: location,
tokenSrc: tokenSrc,
templateBuilder: templateBuilder,
}, nil
}