1 package testutils
2
3 import (
4 "encoding/json"
5 "fmt"
6 "strings"
7 "time"
8
9 secretmanagerpb "cloud.google.com/go/secretmanager/apiv1/secretmanagerpb"
10 "google.golang.org/grpc/codes"
11 "google.golang.org/grpc/status"
12 "google.golang.org/protobuf/types/known/timestamppb"
13
14 "edge-infra.dev/pkg/edge/api/types"
15 "edge-infra.dev/pkg/edge/constants"
16 )
17
18 const (
19 secretManagerKeyFormat = "secret-manager-%s-%s"
20 )
21
22 var (
23 SecretManagerMap = make(map[string][]byte)
24 ts = []*types.SecretInfo{{
25 Name: constants.DockerPullCfg,
26 ProjectID: "foreman",
27 Secret: &secretmanagerpb.Secret{
28 Name: constants.DockerPullCfg,
29 Labels: map[string]string{"test": "test"},
30 CreateTime: ×tamppb.Timestamp{
31 Seconds: time.Now().Unix(),
32 },
33 },
34 SecretValue: []byte("ZG9ja2VyIGNvbmZpZyB2YWxz"),
35 SecretVersion: &secretmanagerpb.SecretVersion{
36 CreateTime: ×tamppb.Timestamp{
37 Seconds: time.Now().Unix(),
38 },
39 },
40 },
41 }
42 )
43
44 func AddToSecretManager(secretValue []byte, name, organization string, labels map[string]string) error {
45 key := fmt.Sprintf(secretManagerKeyFormat, organization, name)
46 smr, err := json.Marshal(toSecretInfo(secretValue, name, organization, labels))
47 if err != nil {
48 return err
49 }
50 SecretManagerMap[key] = smr
51 return nil
52 }
53
54 func GetFromSecretManager(organization string, name string) ([]*secretmanagerpb.Secret, error) {
55 if name == constants.DockerPullCfg {
56 return []*secretmanagerpb.Secret{ts[0].Secret}, nil
57 }
58 secrets, err := GetSecretInfoFromSecretManager(organization, name)
59 if err != nil {
60 return nil, err
61 }
62 var result []*secretmanagerpb.Secret
63 for _, secret := range secrets {
64 result = append(result, secret.Secret)
65 }
66 return result, nil
67 }
68
69 func GetSecretInfoFromSecretManager(organization string, name string) ([]*types.SecretInfo, error) {
70 var secrets []*types.SecretInfo
71 if name != "" {
72 if name == constants.DockerPullCfg {
73 return ts, nil
74 }
75 key := fmt.Sprintf(secretManagerKeyFormat, organization, name)
76 value := SecretManagerMap[key]
77 if value != nil {
78 secret := &types.SecretInfo{}
79 err := json.Unmarshal(value, secret)
80 if err != nil {
81 return nil, err
82 }
83 secrets = append(secrets, secret)
84 }
85 } else {
86 _key := fmt.Sprintf(secretManagerKeyFormat, organization, "")
87 for key, value := range SecretManagerMap {
88 if strings.HasPrefix(key, _key) {
89 secret := &types.SecretInfo{}
90 err := json.Unmarshal(value, secret)
91 if err != nil {
92 return nil, err
93 }
94 secrets = append(secrets, secret)
95 }
96 }
97 }
98 if len(secrets) == 0 {
99 return nil, status.Error(codes.NotFound, "not found")
100 }
101 return secrets, nil
102 }
103
104 func DeleteFromSecretManager(organization, name string) error {
105 key := fmt.Sprintf(secretManagerKeyFormat, organization, name)
106 if _, ok := SecretManagerMap[key]; ok {
107 delete(SecretManagerMap, key)
108 return nil
109 }
110 return status.Error(codes.NotFound, "not found")
111 }
112
113 func toSecretInfo(secretValue []byte, name, organization string, labels map[string]string) *types.SecretInfo {
114 sn := fmt.Sprintf("/api/v1/namespaces/%s/secrets/%s", organization, name)
115 return &types.SecretInfo{
116 Name: name,
117 ProjectID: "test-project",
118 Secret: &secretmanagerpb.Secret{
119 Name: sn,
120 Labels: labels,
121 CreateTime: ×tamppb.Timestamp{
122 Seconds: time.Now().Unix(),
123 },
124 },
125 SecretValue: secretValue,
126 SecretVersion: &secretmanagerpb.SecretVersion{
127 CreateTime: ×tamppb.Timestamp{
128 Seconds: time.Now().Unix(),
129 },
130 },
131 }
132 }
133
View as plain text