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

sgcloudrun: avoid creds.json conflict #587

Merged
merged 2 commits into from
Sep 17, 2024
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
3 changes: 1 addition & 2 deletions sg/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ func ContextWithEnv(ctx context.Context, env ...string) context.Context {

// Command should be used when returning exec.Cmd from tools to set opinionated standard fields.
func Command(ctx context.Context, path string, args ...string) *exec.Cmd {
// TODO: use exec.CommandContext when we have determined there are no side-effects.
cmd := exec.Command(path)
cmd := exec.CommandContext(ctx, path)
cmd.Args = append(cmd.Args, args...)
cmd.Dir = FromGitRoot(".")
cmd.Env = os.Environ()
Expand Down
12 changes: 12 additions & 0 deletions sg/initfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ func generateInitFile(g *codegen.File, pkg *doc.Package, mks []Makefile) error {
}
g.P("logger := ", g.Import("go.einride.tech/sage/sg"), ".NewLogger(\"", loggerName, "\")")
g.P("ctx = ", g.Import("go.einride.tech/sage/sg"), ".WithLogger(ctx, logger)")
g.P("ctx, cancel := context.WithCancel(ctx)")
g.P("defer cancel()")

g.P("go func() {")
g.P("shutdownCh := make(chan ", g.Import("os"), ".Signal, 1)")
g.P(g.Import("os/signal"), ".Notify(shutdownCh, ",
g.Import("os"), ".Interrupt, ",
g.Import("syscall"), ".SIGTERM)",
)
g.P("<-shutdownCh")
g.P("cancel()")
alethenorio marked this conversation as resolved.
Show resolved Hide resolved
g.P("}()")
if len(function.Decl.Type.Params.List) > 1 {
expected := countParams(function.Decl.Type.Params.List) - 1
g.P("if len(args) != ", expected, " {")
Expand Down
20 changes: 19 additions & 1 deletion tools/sgcloudrun/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"io"
"math/rand/v2"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -128,7 +129,7 @@ func LocalDevelopCommand(
if err := os.MkdirAll(workDir, 0o755); err != nil {
return nil, fmt.Errorf("unable to create path to store gcloud credentials: %v", err)
}
credsPath := filepath.Join(workDir, "creds.json")
credsPath := filepath.Join(workDir, fmt.Sprintf("creds-%s.json", randomLower(5)))
if err := os.WriteFile(credsPath, delegateCredsJSON, 0o600); err != nil {
return nil, err
}
Expand All @@ -140,6 +141,12 @@ func LocalDevelopCommand(
cmd.Env = append(cmd.Env, "GOOGLE_APPLICATION_CREDENTIALS="+credsPath)
cmd.Env = append(cmd.Env, env...)
cmd.Env = append(cmd.Env, os.Environ()...) // allow environment overrides
cmd.Cancel = func() error {
if err := cmd.Process.Kill(); err != nil {
return err
}
return os.Remove(credsPath)
}
return cmd, nil
}

Expand Down Expand Up @@ -370,3 +377,14 @@ func fetchImpersonatedAccessToken(ctx context.Context, serviceAccountEmail strin

return tokens.AccessToken, nil
}

func randomLower(n uint32) string {
b := make([]rune, n)
for i := range b {
// NOTE: code of 'a' is 97, code of 'z' is 122.
// so rand.IntN(26) + 97 is code of a "random" lowercase rune.
//nolint:gosec // we don't need a secure randomizer for this.
b[i] = rune(rand.IntN(26) + 97)
}
return string(b)
}