This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
api.go
132 lines (108 loc) · 5.45 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package api
import (
"github.com/caos/orbos/internal/operator/boom/api/v1beta1"
orbconfig "github.com/caos/orbos/internal/orb"
"github.com/caos/orbos/internal/secret"
"github.com/caos/orbos/internal/tree"
"github.com/caos/orbos/mntr"
"github.com/pkg/errors"
"strings"
)
func ParseToolset(desiredTree *tree.Tree, masterkey string) (*v1beta1.Toolset, error) {
desiredKind := v1beta1.New(masterkey)
if err := desiredTree.Original.Decode(desiredKind); err != nil {
return nil, errors.Wrap(err, "parsing desired state failed")
}
err := desiredKind.InitSecretLists(masterkey)
return desiredKind, err
}
func SecretsFunc(orb *orbconfig.Orb) secret.Func {
return func(monitor mntr.Monitor, desiredTree *tree.Tree) (secrets map[string]*secret.Secret, err error) {
defer func() {
err = errors.Wrapf(err, "building %s failed", desiredTree.Common.Kind)
}()
desiredKind, err := ParseToolset(desiredTree, orb.Masterkey)
if err != nil {
return nil, errors.Wrap(err, "parsing desired state failed")
}
desiredTree.Parsed = desiredKind
return getSecretsMap(desiredKind, orb.Masterkey), nil
}
}
func RewriteFunc(orb *orbconfig.Orb, newMasterkey string) secret.Func {
return func(monitor mntr.Monitor, desiredTree *tree.Tree) (secrets map[string]*secret.Secret, err error) {
defer func() {
err = errors.Wrapf(err, "building %s failed", desiredTree.Common.Kind)
}()
desiredKind, err := ParseToolset(desiredTree, orb.Masterkey)
if err != nil {
return nil, errors.Wrap(err, "parsing desired state failed")
}
desiredKind = v1beta1.ReplaceMasterkey(desiredKind, newMasterkey)
desiredTree.Parsed = desiredKind
return getSecretsMap(desiredKind, orb.Masterkey), nil
}
}
func getSecretsMap(desiredKind *v1beta1.Toolset, masterkey string) map[string]*secret.Secret {
ret := map[string]*secret.Secret{
"argocd.sso.google.clientid": desiredKind.Spec.Argocd.Auth.GoogleConnector.Config.ClientID,
"argocd.sso.google.clientsecret": desiredKind.Spec.Argocd.Auth.GoogleConnector.Config.ClientSecret,
"argocd.sso.google.serviceaccountjson": desiredKind.Spec.Argocd.Auth.GoogleConnector.Config.ServiceAccountJSON,
"argocd.sso.gitlab.clientid": desiredKind.Spec.Argocd.Auth.GitlabConnector.Config.ClientID,
"argocd.sso.gitlab.clientsecret": desiredKind.Spec.Argocd.Auth.GitlabConnector.Config.ClientSecret,
"argocd.sso.github.clientid": desiredKind.Spec.Argocd.Auth.GithubConnector.Config.ClientID,
"argocd.sso.github.clientsecret": desiredKind.Spec.Argocd.Auth.GithubConnector.Config.ClientSecret,
"argocd.sso.oidc.clientid": desiredKind.Spec.Argocd.Auth.OIDC.ClientID,
"argocd.sso.oidc.clientsecret": desiredKind.Spec.Argocd.Auth.OIDC.ClientSecret,
"grafana.sso.oauth.clientid": desiredKind.Spec.Grafana.Auth.GenericOAuth.ClientID,
"grafana.sso.oauth.clientsecret": desiredKind.Spec.Grafana.Auth.GenericOAuth.ClientSecret,
"grafana.sso.google.clientid": desiredKind.Spec.Grafana.Auth.Google.ClientID,
"grafana.sso.google.clientsecret": desiredKind.Spec.Grafana.Auth.Google.ClientSecret,
"grafana.sso.github.clientid": desiredKind.Spec.Grafana.Auth.Github.ClientID,
"grafana.sso.github.clientsecret": desiredKind.Spec.Grafana.Auth.Github.ClientSecret,
"grafana.sso.gitlab.clientid": desiredKind.Spec.Grafana.Auth.Gitlab.ClientID,
"grafana.sso.gitlab.clientsecret": desiredKind.Spec.Grafana.Auth.Gitlab.ClientSecret,
"grafana.admin.username": desiredKind.Spec.Grafana.Admin.Username,
"grafana.admin.password": desiredKind.Spec.Grafana.Admin.Password,
}
if desiredKind.Spec.Argocd != nil && desiredKind.Spec.Argocd.Credentials != nil {
for _, value := range desiredKind.Spec.Argocd.Credentials {
base := strings.Join([]string{"argocd", "credential", value.Name}, ".")
key := strings.Join([]string{base, "username"}, ".")
value.Username = secret.InitIfNil(value.Username, masterkey)
ret[key] = value.Username
key = strings.Join([]string{base, "password"}, ".")
value.Password = secret.InitIfNil(value.Password, masterkey)
ret[key] = value.Password
key = strings.Join([]string{base, "certificate"}, ".")
value.Certificate = secret.InitIfNil(value.Certificate, masterkey)
ret[key] = value.Certificate
}
}
if desiredKind.Spec.Argocd != nil && desiredKind.Spec.Argocd.Repositories != nil {
for _, value := range desiredKind.Spec.Argocd.Repositories {
base := strings.Join([]string{"argocd", "repository", value.Name}, ".")
key := strings.Join([]string{base, "username"}, ".")
value.Username = secret.InitIfNil(value.Username, masterkey)
ret[key] = value.Username
key = strings.Join([]string{base, "password"}, ".")
value.Password = secret.InitIfNil(value.Password, masterkey)
ret[key] = value.Password
key = strings.Join([]string{base, "certificate"}, ".")
value.Certificate = secret.InitIfNil(value.Certificate, masterkey)
ret[key] = value.Certificate
}
}
if desiredKind.Spec.Argocd != nil && desiredKind.Spec.Argocd.CustomImage != nil && desiredKind.Spec.Argocd.CustomImage.GopassStores != nil {
for _, value := range desiredKind.Spec.Argocd.CustomImage.GopassStores {
base := strings.Join([]string{"argocd", "gopass", value.StoreName}, ".")
key := strings.Join([]string{base, "ssh"}, ".")
value.SSHKey = secret.InitIfNil(value.SSHKey, masterkey)
ret[key] = value.SSHKey
key = strings.Join([]string{base, "gpg"}, ".")
value.GPGKey = secret.InitIfNil(value.GPGKey, masterkey)
ret[key] = value.GPGKey
}
}
return ret
}