-
Notifications
You must be signed in to change notification settings - Fork 9
/
providers_create.go
148 lines (130 loc) · 3.6 KB
/
providers_create.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package cd
import (
"encoding/json"
"fmt"
"os"
"github.com/AlecAivazis/survey/v2"
gqlclient "github.com/pluralsh/console-client-go"
"github.com/pluralsh/plural-cli/pkg/api"
)
func AskCloudProviderSettings(provider string) (*gqlclient.CloudProviderSettingsAttributes, error) {
switch provider {
case api.ProviderAWS:
if acs, err := askAWSCloudProviderSettings(); err != nil {
return nil, err
} else {
return &gqlclient.CloudProviderSettingsAttributes{Aws: acs}, nil
}
case api.ProviderAzure:
if acs, err := askAzureCloudProviderSettings(); err != nil {
return nil, err
} else {
return &gqlclient.CloudProviderSettingsAttributes{Azure: acs}, nil
}
case api.ProviderGCP:
if gcs, err := askGCPCloudProviderSettings(); err != nil {
return nil, err
} else {
return &gqlclient.CloudProviderSettingsAttributes{Gcp: gcs}, nil
}
}
return nil, fmt.Errorf("unknown provider")
}
func askAWSCloudProviderSettings() (*gqlclient.AwsSettingsAttributes, error) {
awsSurvey := []*survey.Question{
{
Name: "key",
Prompt: &survey.Input{Message: "Enter the Access Key ID:"},
},
{
Name: "secret",
Prompt: &survey.Input{Message: "Enter Secret Access Key:"},
},
}
var resp struct {
Key string
Secret string
}
if err := survey.Ask(awsSurvey, &resp); err != nil {
return nil, err
}
return &gqlclient.AwsSettingsAttributes{
AccessKeyID: resp.Key,
SecretAccessKey: resp.Secret,
}, nil
}
func askAzureCloudProviderSettings() (*gqlclient.AzureSettingsAttributes, error) {
azureSurvey := []*survey.Question{
{
Name: "tenant",
Prompt: &survey.Input{Message: "Enter the tenant ID:"},
},
{
Name: "client",
Prompt: &survey.Input{Message: "Enter the client ID:"},
},
{
Name: "secret",
Prompt: &survey.Input{Message: "Enter the client secret:"},
},
{
Name: "subscription",
Prompt: &survey.Input{Message: "Enter the subscription ID:"},
},
}
var resp struct {
Tenant string
Client string
Secret string
Subscription string
}
if err := survey.Ask(azureSurvey, &resp); err != nil {
return nil, err
}
return &gqlclient.AzureSettingsAttributes{
TenantID: resp.Tenant,
ClientID: resp.Client,
ClientSecret: resp.Secret,
SubscriptionID: resp.Subscription,
}, nil
}
func askGCPCloudProviderSettings() (*gqlclient.GcpSettingsAttributes, error) {
applicationCredentialsFilePath := ""
prompt := &survey.Input{
Message: "Enter GCP application credentials file path:",
}
if err := survey.AskOne(prompt, &applicationCredentialsFilePath, survey.WithValidator(validServiceAccountCredentials)); err != nil {
return nil, err
}
return &gqlclient.GcpSettingsAttributes{
ApplicationCredentials: toCredentialsJSON(applicationCredentialsFilePath),
}, nil
}
type credentials struct {
Email string `json:"client_email"`
ID string `json:"client_id"`
Type credentialsType `json:"type"`
}
type credentialsType = string
const (
ServiceAccountType credentialsType = "service_account"
)
func validServiceAccountCredentials(val interface{}) error {
path, _ := val.(string)
bytes, err := os.ReadFile(path)
if err != nil {
return err
}
creds := new(credentials)
if err = json.Unmarshal(bytes, creds); err != nil {
return err
}
if creds.Type != ServiceAccountType || len(creds.Email) == 0 || len(creds.ID) == 0 {
return fmt.Errorf("provided credentials file is not a valid service account. Must have type 'service_account' and both 'client_id' and 'client_email' set")
}
return nil
}
func toCredentialsJSON(path string) string {
bytes, _ := os.ReadFile(path)
return string(bytes)
}