internal/secret: add a must constructor for the secret client

Multiple packages have a need for a secret client which exits when the
creation of the client failed. This change adds a must constructor
which would eliminate the code being duplicated.

For  golang/go#37171

Change-Id: I7f56ee681e66c42e290fa00861cb00abb56a2f47
Reviewed-on: https://go-review.googlesource.com/c/build/+/255941
Trust: Carlos Amedee <carlos@golang.org>
Trust: Alexander Rakoczy <alex@golang.org>
Run-TryBot: Carlos Amedee <carlos@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alexander Rakoczy <alex@golang.org>
This commit is contained in:
Carlos Amedee 2020-09-18 14:28:07 -04:00
Родитель f1e0897840
Коммит 442b1a7d81
9 изменённых файлов: 19 добавлений и 65 удалений

Просмотреть файл

@ -4064,11 +4064,7 @@ func mustCreateSecretClientOnGCE() *secret.Client {
if !metadata.OnGCE() {
return nil
}
client, err := secret.NewClient()
if err != nil {
log.Fatalf("unable to create secret client %v", err)
}
return client
return secret.MustNewClient()
}
func mustCreateEC2BuildletPool(sc *secret.Client) *pool.EC2Buildlet {

Просмотреть файл

@ -56,13 +56,8 @@ func main() {
var secretClient *secret.Client
if metadata.OnGCE() {
var err error
secretClient, err = secret.NewClient()
if err != nil {
log.Fatalf("unable to create a secret manager client: %v", err)
}
secretClient = secret.MustNewClient()
}
if err := writeCookiesFile(secretClient); err != nil {
log.Fatalf("writeCookiesFile(): %v", err)
}

Просмотреть файл

@ -57,7 +57,7 @@ func main() {
log.Printf("gitmirror running.")
sc := mustCreateSecretClient()
sc := secret.MustNewClient()
defer sc.Close()
go pollGerritAndTickle()
@ -769,11 +769,3 @@ func handleDebugEnv(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s\n", kv)
}
}
func mustCreateSecretClient() *secret.Client {
client, err := secret.NewClient()
if err != nil {
log.Fatalf("unable to create secret client %v", err)
}
return client
}

Просмотреть файл

@ -197,7 +197,7 @@ func main() {
var sc *secret.Client
if metadata.OnGCE() {
sc = mustCreateSecretClient()
sc = secret.MustNewClient()
}
ctx := context.Background()
@ -2311,11 +2311,3 @@ func printIssue(task string, repoID maintner.GitHubRepoID, gi *maintner.GitHubIs
fmt.Printf("\thttps://golang.org/issue/%v %s\n", gi.Number, gi.Title)
}
}
func mustCreateSecretClient() *secret.Client {
client, err := secret.NewClient()
if err != nil {
log.Fatalf("unable to create secret client %v", err)
}
return client
}

Просмотреть файл

@ -58,7 +58,7 @@ func main() {
// webhooksecret should not be set in production
if *webhookSecret == "" {
sc := mustCreateSecretClient()
sc := secret.MustNewClient()
defer sc.Close()
ctxSc, cancel := context.WithTimeout(ctx, 10*time.Second)
@ -428,11 +428,3 @@ func onNewConnection(c smtpd.Connection) error {
log.Printf("smtpd: new connection from %v", c.Addr())
return nil
}
func mustCreateSecretClient() *secret.Client {
client, err := secret.NewClient()
if err != nil {
log.Fatalf("unable to create secret client %v", err)
}
return client
}

Просмотреть файл

@ -55,7 +55,7 @@ const (
func main() {
flag.Parse()
secretClient := mustCreateSecretClient()
secretClient := secret.MustNewClient()
defer secretClient.Close()
if *tags == "" && !*listAll { // Tags aren't needed if -list-all flag is set.
@ -413,11 +413,3 @@ func getConnectedMachines() map[string]*revtype.ReverseBuilder {
}
return st.Machines
}
func mustCreateSecretClient() *secret.Client {
client, err := secret.NewClient()
if err != nil {
log.Fatalf("unable to create secret client %v", err)
}
return client
}

Просмотреть файл

@ -32,7 +32,7 @@ func Init() error {
return nil
}
sc := mustCreateSecretClient()
sc := secret.MustNewClient()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
@ -59,11 +59,3 @@ func homeDir() string {
log.Fatalf("No HOME set in environment.")
panic("unreachable")
}
func mustCreateSecretClient() *secret.Client {
client, err := secret.NewClient()
if err != nil {
log.Fatalf("unable to create secret client %v", err)
}
return client
}

Просмотреть файл

@ -9,6 +9,7 @@ package secret
import (
"context"
"io"
"log"
"path"
"cloud.google.com/go/compute/metadata"
@ -105,3 +106,13 @@ func (smc *Client) Close() error {
func buildNamePath(projectID, name, version string) string {
return path.Join("projects", projectID, "secrets", name, "versions", version)
}
// MustNewClient instantiates an instance of the Secret Manager Client. If there is an error
// this fuction will exit.
func MustNewClient() *Client {
c, err := NewClient()
if err != nil {
log.Fatalf("unable to create secret client %v", err)
}
return c
}

Просмотреть файл

@ -366,7 +366,7 @@ func setGodataConfig() {
func getGithubToken(ctx context.Context) (string, error) {
if metadata.OnGCE() {
sc := mustCreateSecretClient()
sc := secret.MustNewClient()
ctxSc, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
@ -503,11 +503,3 @@ func syncProdToDevMutationLogs() {
}
}
}
func mustCreateSecretClient() *secret.Client {
client, err := secret.NewClient()
if err != nil {
log.Fatalf("unable to create secret client %v", err)
}
return client
}