-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud_account.go
More file actions
173 lines (150 loc) · 4.79 KB
/
cloud_account.go
File metadata and controls
173 lines (150 loc) · 4.79 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package module
import (
"context"
"fmt"
"github.com/GoCodeAlone/modular"
)
// CloudCredentialProvider provides cloud credentials to other modules.
type CloudCredentialProvider interface {
Provider() string
Region() string
GetCredentials(ctx context.Context) (*CloudCredentials, error)
}
// CloudCredentials holds resolved credentials for a cloud provider.
type CloudCredentials struct {
Provider string
Region string
// AWS
AccessKey string //nolint:gosec // G117: config struct field, not a hardcoded secret
SecretKey string
SessionToken string //nolint:gosec // G117: config struct field, not a hardcoded secret
RoleARN string
// GCP
ProjectID string
ServiceAccountJSON []byte
// Azure
TenantID string
ClientID string
ClientSecret string //nolint:gosec // G117: config struct field, not a hardcoded secret
SubscriptionID string
// Kubernetes
Kubeconfig []byte
Context string
// Generic / DigitalOcean
Token string
Extra map[string]string
}
// CloudAccount is a workflow module that stores cloud provider credentials
// and exposes them via the CloudCredentialProvider interface in the service registry.
type CloudAccount struct {
name string
config map[string]any
provider string
region string
credType string
creds *CloudCredentials
}
// NewCloudAccount creates a new CloudAccount module.
func NewCloudAccount(name string, cfg map[string]any) *CloudAccount {
return &CloudAccount{name: name, config: cfg}
}
// Name returns the module name.
func (m *CloudAccount) Name() string { return m.name }
// Init resolves credentials and registers the module as a service.
func (m *CloudAccount) Init(app modular.Application) error {
m.provider, _ = m.config["provider"].(string)
if m.provider == "" {
m.provider = "mock"
}
m.region, _ = m.config["region"].(string)
var err error
m.creds, err = m.resolveCredentials()
if err != nil {
return fmt.Errorf("cloud.account %q: failed to resolve credentials: %w", m.name, err)
}
return app.RegisterService(m.name, m)
}
// ProvidesServices declares the service this module provides.
func (m *CloudAccount) ProvidesServices() []modular.ServiceProvider {
return []modular.ServiceProvider{
{
Name: m.name,
Description: "Cloud account: " + m.name,
Instance: m,
},
}
}
// RequiresServices returns nil — cloud.account has no service dependencies.
func (m *CloudAccount) RequiresServices() []modular.ServiceDependency {
return nil
}
// Provider returns the cloud provider name (e.g. "aws", "gcp", "mock").
func (m *CloudAccount) Provider() string { return m.provider }
// Region returns the primary region.
func (m *CloudAccount) Region() string { return m.region }
// GetCredentials returns the resolved credentials.
func (m *CloudAccount) GetCredentials(_ context.Context) (*CloudCredentials, error) {
if m.creds == nil {
return nil, fmt.Errorf("cloud.account %q: credentials not initialized", m.name)
}
return m.creds, nil
}
// resolveCredentials resolves credentials based on provider and credential type config.
// It dispatches to registered CloudCredentialResolvers via the global registry.
func (m *CloudAccount) resolveCredentials() (*CloudCredentials, error) {
creds := &CloudCredentials{
Provider: m.provider,
Region: m.region,
}
// Read top-level provider-specific config fields.
if pid, ok := m.config["project_id"].(string); ok {
creds.ProjectID = pid
}
if sid, ok := m.config["subscription_id"].(string); ok {
creds.SubscriptionID = sid
}
if m.provider == "mock" {
return m.resolveMockCredentials(creds)
}
credsMap, _ := m.config["credentials"].(map[string]any)
if credsMap == nil {
// No credentials configured — return empty (valid for some providers)
return creds, nil
}
m.credType, _ = credsMap["type"].(string)
if m.credType == "" {
m.credType = "static"
}
// Store creds on m so resolvers can write into it directly.
m.creds = creds
providerResolvers, ok := credentialResolvers[m.provider]
if !ok {
return nil, fmt.Errorf("unknown cloud provider: %s", m.provider)
}
resolver, ok := providerResolvers[m.credType]
if !ok {
return nil, fmt.Errorf("unsupported credential type %q for provider %q", m.credType, m.provider)
}
if err := resolver.Resolve(m); err != nil {
return nil, err
}
return m.creds, nil
}
func (m *CloudAccount) resolveMockCredentials(creds *CloudCredentials) (*CloudCredentials, error) {
credsMap, _ := m.config["credentials"].(map[string]any)
if credsMap != nil {
creds.AccessKey, _ = credsMap["accessKey"].(string)
creds.SecretKey, _ = credsMap["secretKey"].(string)
creds.Token, _ = credsMap["token"].(string)
}
if creds.AccessKey == "" {
creds.AccessKey = "mock-access-key"
}
if creds.SecretKey == "" {
creds.SecretKey = "mock-secret-key"
}
if creds.Region == "" {
creds.Region = "us-mock-1"
}
return creds, nil
}