forked from openshift/osd-network-verifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.go
More file actions
248 lines (211 loc) · 9.78 KB
/
cmd.go
File metadata and controls
248 lines (211 loc) · 9.78 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package egress
import (
"bytes"
"context"
"fmt"
"os"
"time"
"github.com/openshift/osd-network-verifier/cmd/utils"
"github.com/openshift/osd-network-verifier/pkg/helpers"
"github.com/openshift/osd-network-verifier/pkg/proxy"
"github.com/openshift/osd-network-verifier/pkg/verifier"
gcpverifier "github.com/openshift/osd-network-verifier/pkg/verifier/gcp"
"golang.org/x/oauth2/google"
"github.com/spf13/cobra"
)
var (
awsDefaultTags = map[string]string{"osd-network-verifier": "owned", "red-hat-managed": "true", "Name": "osd-network-verifier"}
gcpDefaultTags = map[string]string{"osd-network-verifier": "owned", "red-hat-managed": "true", "name": "osd-network-verifier"}
awsRegionEnvVarStr = "AWS_REGION"
awsRegionDefault = "us-east-2"
gcpRegionEnvVarStr = "GCP_REGION"
gcpRegionDefault = "us-east1"
platformTypeDefault = helpers.PlatformAWS
)
type egressConfig struct {
vpcSubnetID string
cloudImageID string
instanceType string
securityGroupId string
cloudTags map[string]string
debug bool
region string
timeout time.Duration
kmsKeyID string
httpProxy string
httpsProxy string
CaCert string
noTls bool
platformType string
awsProfile string
gcpVpcName string
skipAWSInstanceTermination bool
terminateDebugInstance string
importKeyPair string
}
func getDefaultRegion(platformType string) string {
if platformType == helpers.PlatformGCP {
//gcp region
dRegion, ok := os.LookupEnv(gcpRegionEnvVarStr)
if !ok {
return gcpRegionDefault
}
return dRegion
}
//aws region
dRegion, ok := os.LookupEnv(awsRegionEnvVarStr)
if !ok {
return awsRegionDefault
}
return dRegion
}
func NewCmdValidateEgress() *cobra.Command {
config := egressConfig{}
validateEgressCmd := &cobra.Command{
Use: "egress",
Short: "Verify essential OpenShift domains are reachable from given subnet ID.",
Long: `Verify essential OpenShift domains are reachable from given subnet ID.`,
Example: `For AWS, ensure your credential environment vars
AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY (also AWS_SESSION_TOKEN for STS credentials)
are set correctly before execution.
# Verify that essential OpenShift domains are reachable from a given SUBNET_ID/SECURITY_GROUP association
./osd-network-verifier egress --subnet-id ${SUBNET_ID} --security-group-id ${SECURITY_GROUP}`,
Run: func(cmd *cobra.Command, args []string) {
// Set Region
if config.region == "" {
config.region = getDefaultRegion(config.platformType)
}
// Set Up Proxy
if config.CaCert != "" {
// Read in the cert file
cert, err := os.ReadFile(config.CaCert)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// store string form of it
// this was agreed with sda that they'll be communicating it as a string.
config.CaCert = bytes.NewBuffer(cert).String()
}
p := proxy.ProxyConfig{
HttpProxy: config.httpProxy,
HttpsProxy: config.httpsProxy,
Cacert: config.CaCert,
NoTls: config.noTls,
}
// setup non cloud config options
vei := verifier.ValidateEgressInput{
Ctx: context.TODO(),
SubnetID: config.vpcSubnetID,
CloudImageID: config.cloudImageID,
Timeout: config.timeout,
Tags: config.cloudTags,
InstanceType: config.instanceType,
PlatformType: config.platformType,
Proxy: p,
}
// AWS workflow
if config.platformType == helpers.PlatformAWS || config.platformType == helpers.PlatformHostedCluster {
if len(vei.Tags) == 0 {
vei.Tags = awsDefaultTags
}
//Setup AWS Specific Configs
vei.AWS = verifier.AwsEgressConfig{
KmsKeyID: config.kmsKeyID,
SecurityGroupId: config.securityGroupId,
}
awsVerifier, err := utils.GetAwsVerifier(config.region, config.awsProfile, config.debug)
if err != nil {
fmt.Printf("could not build awsVerifier %v\n", err)
os.Exit(1)
}
awsVerifier.Logger.Warn(context.TODO(), "Using region: %s", config.region)
vei.SkipInstanceTermination = config.skipAWSInstanceTermination
vei.TerminateDebugInstance = config.terminateDebugInstance
vei.ImportKeyPair = config.importKeyPair
out := verifier.ValidateEgress(awsVerifier, vei)
out.Summary(config.debug)
if !out.IsSuccessful() {
awsVerifier.Logger.Error(context.TODO(), "Failure!")
os.Exit(1)
}
awsVerifier.Logger.Info(context.TODO(), "Success")
os.Exit(0)
}
// GCP workflow
if config.platformType == helpers.PlatformGCP {
if len(vei.Tags) == 0 {
vei.Tags = gcpDefaultTags
}
projectID := os.Getenv("GCP_PROJECT_ID")
if projectID == "" {
fmt.Println("please set environment variable GCP_PROJECT_ID to the project ID of the VPC")
os.Exit(1)
}
vpcName := config.gcpVpcName
if vpcName == "" {
fmt.Println("please pass the flag --vpc-name=<VPC-NAME> to identify the VPC")
os.Exit(1)
}
//Setup GCP Secific Configs
vei.GCP = verifier.GcpEgressConfig{
Region: config.region,
//Zone b is supported by all regions and has the most machine types compared to zone a and c
//https://cloud.google.com/compute/docs/regions-zones#available
Zone: fmt.Sprintf("%s-b", config.region),
ProjectID: projectID,
VpcName: vpcName,
}
// Tries to find google credentials in all known locations stating with env "GOOGLE_APPLICATION_CREDENTIALS""
creds, err := google.FindDefaultCredentials(context.TODO())
if err != nil {
fmt.Printf("could not find GCP credentials file: %v\n", err)
os.Exit(1)
}
gcpVerifier, err := gcpverifier.NewGcpVerifier(creds, config.debug)
if err != nil {
fmt.Printf("could not build GcpVerifier: %v\n", err)
os.Exit(1)
}
gcpVerifier.Logger.Info(context.TODO(), "Using Project ID %s", vei.GCP.ProjectID)
out := verifier.ValidateEgress(gcpVerifier, vei)
out.Summary(config.debug)
if !out.IsSuccessful() {
gcpVerifier.Logger.Error(context.TODO(), "Failure!")
os.Exit(1)
}
gcpVerifier.Logger.Info(context.TODO(), "Success")
os.Exit(0)
}
// Unknown platformType specified
fmt.Printf("unknown platform type '%v'\n", config.platformType)
os.Exit(1)
},
}
validateEgressCmd.Flags().StringVar(&config.platformType, "platform", platformTypeDefault, fmt.Sprintf("(optional) infra platform type, which determines which endpoints to test. Either '%v', '%v', or '%v' (hypershift)", helpers.PlatformAWS, helpers.PlatformGCP, helpers.PlatformHostedCluster))
validateEgressCmd.Flags().StringVar(&config.vpcSubnetID, "subnet-id", "", "source subnet ID")
validateEgressCmd.Flags().StringVar(&config.cloudImageID, "image-id", "", "(optional) cloud image for the compute instance")
validateEgressCmd.Flags().StringVar(&config.instanceType, "instance-type", "", "(optional) compute instance type")
validateEgressCmd.Flags().StringVar(&config.securityGroupId, "security-group-id", "", "security group ID to attach to the created EC2 instance")
validateEgressCmd.Flags().StringVar(&config.region, "region", "", fmt.Sprintf("(optional) compute instance region. If absent, environment var %[1]v = %[2]v and %[3]v = %[4]v will be used", awsRegionEnvVarStr, awsRegionDefault, gcpRegionEnvVarStr, gcpRegionDefault))
validateEgressCmd.Flags().StringToStringVar(&config.cloudTags, "cloud-tags", map[string]string{}, "(optional) comma-seperated list of tags to assign to cloud resources e.g. --cloud-tags key1=value1,key2=value2")
validateEgressCmd.Flags().BoolVar(&config.debug, "debug", false, "(optional) if true, enable additional debug-level logging")
validateEgressCmd.Flags().DurationVar(&config.timeout, "timeout", 2*time.Second, "(optional) timeout for individual egress verification requests")
validateEgressCmd.Flags().StringVar(&config.kmsKeyID, "kms-key-id", "", "(optional) ID of KMS key used to encrypt root volumes of compute instances. Defaults to cloud account default key")
validateEgressCmd.Flags().StringVar(&config.httpProxy, "http-proxy", "", "(optional) http-proxy to be used upon http requests being made by verifier, format: http://user:pass@x.x.x.x:8978")
validateEgressCmd.Flags().StringVar(&config.httpsProxy, "https-proxy", "", "(optional) https-proxy to be used upon https requests being made by verifier, format: https://user:pass@x.x.x.x:8978")
validateEgressCmd.Flags().StringVar(&config.CaCert, "cacert", "", "(optional) path to cacert file to be used upon https requests being made by verifier")
validateEgressCmd.Flags().BoolVar(&config.noTls, "no-tls", false, "(optional) if true, skip client-side SSL certificate validation")
validateEgressCmd.Flags().StringVar(&config.awsProfile, "profile", "", "(optional) AWS profile. If present, any credentials passed with CLI will be ignored")
validateEgressCmd.Flags().StringVar(&config.gcpVpcName, "vpc-name", "", "(optional unless --platform='gcp') VPC name where GCP cluster is installed")
validateEgressCmd.Flags().BoolVar(&config.skipAWSInstanceTermination, "skip-termination", false, "(optional) Skip instance termination to allow further debugging")
validateEgressCmd.Flags().StringVar(&config.terminateDebugInstance, "terminate-debug", "", "(optional) Takes the debug instance ID and terminates it")
validateEgressCmd.Flags().StringVar(&config.importKeyPair, "import-keypair", "", "(optional) Takes the path to your public key used to connect to Debug Instance. Automatically skips Termination")
if err := validateEgressCmd.MarkFlagRequired("subnet-id"); err != nil {
validateEgressCmd.PrintErr(err)
}
if err := validateEgressCmd.MarkFlagRequired("security-group-id"); err != nil {
validateEgressCmd.PrintErr(err)
}
return validateEgressCmd
}