-
Notifications
You must be signed in to change notification settings - Fork 197
/
commonparams.go
151 lines (127 loc) · 6.13 KB
/
commonparams.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
149
150
151
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package commonparams
import (
"fmt"
"strings"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/cli/outputsink"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/cli/stream"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/valutil"
"github.com/spf13/cobra"
)
type IAMFormatOption string
type ResourceFormatOption string
const (
IAMFormatParamName = "iam-format"
FilterDeletedIAMMembersParamName = "filter-deleted-iam-members"
OAuth2TokenParamName = "oauth2-token"
OutputParamName = "output"
ResourceFormatParamName = "resource-format"
PartialPolicyFormatOption = "partialpolicy"
PolicyIAMFormatOption = "policy"
PolicyMemberIAMFormatOption = "policymember"
NoneIAMFormatOption = "none"
KRMResourceFormatOption = outputsink.KRMResourceFormat
HCLResourceFormatOption = outputsink.HCLResourceFormat
IAMFormatDefault = PolicyIAMFormatOption
FilterDeletedIAMMembersDefault = false
OAuth2TokenDefault = ""
OutputDefault = ""
ResourceFormatDefault = KRMResourceFormatOption
OAuth2TokenUsage = "an optional OAuth 2.0 access token to be used as the identity for communication with GCP services, can be obtained with 'gcloud auth print-access-token'"
OutputUsage = "an optional output file path, disables standard output, when a file the result will contain all of the command output, when a directory, the directory will contain a new file for each resource in the output"
)
var (
IAMFormatUsage = fmt.Sprintf("specify the IAM resource format or disable IAM output, options are '%v', '%v', '%v', or '%v'", PartialPolicyFormatOption, PolicyIAMFormatOption, PolicyMemberIAMFormatOption, NoneIAMFormatOption)
FilterDeletedIAMMembersUsage = fmt.Sprintf("specify whether to filter out deleted IAM members, options are '%v' or '%v', (default: '%v')", true, false, FilterDeletedIAMMembersDefault)
ResourceFormatUsage = fmt.Sprintf("specify the format of the outputted resources, options are '%v' or '%v' (default: '%v')", KRMResourceFormatOption, HCLResourceFormatOption, ResourceFormatDefault)
)
func AddOAuth2TokenParam(cmd *cobra.Command, value *string) {
cmd.Flags().StringVar(value, OAuth2TokenParamName, OAuth2TokenDefault, OAuth2TokenUsage)
}
func AddIAMFormatParam(cmd *cobra.Command, value *string) {
cmd.Flags().StringVar(value, IAMFormatParamName, IAMFormatDefault, IAMFormatUsage)
}
func AddFilterDeletedIAMMembersParam(cmd *cobra.Command, value *bool) {
cmd.Flags().BoolVar(value, FilterDeletedIAMMembersParamName, FilterDeletedIAMMembersDefault, FilterDeletedIAMMembersUsage)
}
func ValidateIAMFormat(value string) error {
iamFormatOptions := []string{PartialPolicyFormatOption, PolicyIAMFormatOption, PolicyMemberIAMFormatOption, NoneIAMFormatOption}
if valutil.IsDefaultValue(value) {
return fmt.Errorf("invalid empty value for %v: must be one of {%v}", IAMFormatParamName, strings.Join(iamFormatOptions, ", "))
}
for _, o := range iamFormatOptions {
if value == o {
return nil
}
}
return fmt.Errorf("invalid %v value of '%v': must be one of {%v}", IAMFormatParamName, value, strings.Join(iamFormatOptions, ", "))
}
// Convert from the an IAMPolicyParam supplied on the command line to the stream type
func IAMFormatParamToStreamIAMFormat(iamFormatParam string) (stream.IAMFormat, error) {
switch iamFormatParam {
case PartialPolicyFormatOption:
return stream.IAMFormatPartialPolicy, nil
case PolicyIAMFormatOption:
return stream.IAMFormatPolicy, nil
case PolicyMemberIAMFormatOption:
return stream.IAMFormatPolicyMember, nil
}
return "", fmt.Errorf("invalid policy format option '%v'", iamFormatParam)
}
func AddOutputParam(cmd *cobra.Command, value *string) {
cmd.Flags().StringVar(value, OutputParamName, OutputDefault, OutputUsage)
}
func AddResourceFormatParam(cmd *cobra.Command, value *string) {
cmd.Flags().StringVar(value, ResourceFormatParamName, ResourceFormatDefault, ResourceFormatUsage)
if err := cmd.Flags().MarkHidden(ResourceFormatParamName); err != nil {
panic(err)
}
}
func ValidateResourceFormat(resourceFormat, iamFormat string) error {
if err := validateResourceFormatValue(resourceFormat); err != nil {
return err
}
return validateResourceFormatMutualExclusivity(resourceFormat, iamFormat)
}
func validateResourceFormatValue(value string) error {
resourceFormatOptions := []string{KRMResourceFormatOption, HCLResourceFormatOption}
if valutil.IsDefaultValue(value) {
return fmt.Errorf("invalid empty value for %v: must be one of {%v}", ResourceFormatParamName, strings.Join(resourceFormatOptions, ", "))
}
for _, o := range resourceFormatOptions {
if value == o {
return nil
}
}
return fmt.Errorf("invalid %v value of '%v': must be one of {%v}", ResourceFormatParamName, value, strings.Join(resourceFormatOptions, ", "))
}
func validateResourceFormatMutualExclusivity(resourceFormat, iamFormat string) error {
switch resourceFormat {
case HCLResourceFormatOption:
return validateHCLResourceFormatMutualExclusivity(iamFormat)
case KRMResourceFormatOption:
// all parameters can be used with the KRM format
return nil
default:
return fmt.Errorf("unhandled resource format %v", resourceFormat)
}
}
func validateHCLResourceFormatMutualExclusivity(iamFormat string) error {
if iamFormat == NoneIAMFormatOption {
return nil
}
return fmt.Errorf("unsupported value of '%v' for flag '%v': when '%v' is '%v' the '%v' flag must have a value of '%v'",
iamFormat, IAMFormatParamName, ResourceFormatParamName, HCLResourceFormatOption, IAMFormatParamName, NoneIAMFormatOption)
}