2022-01-13 20:02:24 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-04-04 05:48:43 +00:00
|
|
|
"os"
|
2022-01-13 20:02:24 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
secretmanager "cloud.google.com/go/secretmanager/apiv1"
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Secret struct{ kv map[string]string }
|
|
|
|
|
|
|
|
func (s *Secret) MustGetField(name string) string {
|
|
|
|
val, ok := s.kv[name]
|
|
|
|
if !ok {
|
|
|
|
panic(errors.Errorf("field %s not found", name))
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2022-04-04 05:48:43 +00:00
|
|
|
func GetSecretFromEnv(filename string) (secret *Secret, err error) {
|
|
|
|
data, err := godotenv.Read(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Secret{kv: data}, nil
|
|
|
|
}
|
|
|
|
|
2022-01-13 20:02:24 +00:00
|
|
|
func GetTestSecret(ctx context.Context) (secret *Secret, err error) {
|
2022-04-04 05:48:43 +00:00
|
|
|
filename := os.Getenv("TEST_SECRET_FILE")
|
|
|
|
if len(filename) > 0 {
|
|
|
|
return GetSecretFromEnv(filename)
|
|
|
|
}
|
|
|
|
|
2022-01-13 20:02:24 +00:00
|
|
|
return GetSecret(ctx, "trufflehog-testing", "test")
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSecret(ctx context.Context, gcpProject, name string) (secret *Secret, err error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
|
|
|
|
defer cancel()
|
|
|
|
|
2022-04-04 05:48:43 +00:00
|
|
|
filename := os.Getenv("TEST_SECRET_FILE")
|
|
|
|
if len(filename) > 0 {
|
|
|
|
return GetSecretFromEnv(filename)
|
|
|
|
}
|
|
|
|
|
2022-01-13 20:02:24 +00:00
|
|
|
parent := fmt.Sprintf("projects/%s/secrets/%s/versions/latest", gcpProject, name)
|
|
|
|
|
|
|
|
client, err := secretmanager.NewClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("failed to create secretmanager client: %v", err)
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
req := &secretmanagerpb.AccessSecretVersionRequest{
|
|
|
|
Name: parent,
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := client.AccessSecretVersion(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("failed to access secret version: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := godotenv.Unmarshal(string(result.Payload.Data))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Secret{kv: data}, nil
|
|
|
|
}
|