github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/types/matchers_gcp.go (about) 1 /* 2 Copyright 2023 Gravitational, Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package types 18 19 import ( 20 "slices" 21 22 "github.com/gravitational/trace" 23 24 apiutils "github.com/gravitational/teleport/api/utils" 25 ) 26 27 const ( 28 // GCPInviteTokenName is the name of the default token to use 29 // when templating the script to be executed on GCP. 30 GCPInviteTokenName = "gcp-discovery-token" 31 32 // GCPMatcherKubernetes is the GCP matcher type for GCP kubernetes. 33 GCPMatcherKubernetes = "gke" 34 // GCPMatcherCompute is the GCP matcher for GCP VMs. 35 GCPMatcherCompute = "gce" 36 ) 37 38 // SupportedGCPMatchers is list of GCP services currently supported by the 39 // Teleport discovery service. 40 var SupportedGCPMatchers = []string{ 41 GCPMatcherKubernetes, 42 GCPMatcherCompute, 43 } 44 45 // GetTypes gets the types that the matcher can match. 46 func (m GCPMatcher) GetTypes() []string { 47 return m.Types 48 } 49 50 // CopyWithTypes copies the matcher with new types. 51 func (m GCPMatcher) CopyWithTypes(t []string) Matcher { 52 newMatcher := m 53 newMatcher.Types = t 54 return newMatcher 55 } 56 57 // GetLabels gets the matcher's labels. 58 func (m GCPMatcher) GetLabels() Labels { 59 if len(m.Labels) != 0 { 60 return m.Labels 61 } 62 // Check Tags as well for backwards compatibility. 63 return m.Tags 64 } 65 66 // CheckAndSetDefaults that the matcher is correct and adds default values. 67 func (m *GCPMatcher) CheckAndSetDefaults() error { 68 if len(m.Types) == 0 { 69 return trace.BadParameter("At least one GCP discovery service type must be specified, the supported resource types are: %v", 70 SupportedGCPMatchers) 71 } 72 73 for _, matcherType := range m.Types { 74 if !slices.Contains(SupportedGCPMatchers, matcherType) { 75 return trace.BadParameter("GCP discovery service type does not support %q resource type; supported resource types are: %v", 76 matcherType, SupportedGCPMatchers) 77 } 78 } 79 80 if slices.Contains(m.Types, GCPMatcherCompute) { 81 if m.Params == nil { 82 m.Params = &InstallerParams{} 83 } 84 85 switch m.Params.JoinMethod { 86 case JoinMethodGCP, "": 87 m.Params.JoinMethod = JoinMethodGCP 88 default: 89 return trace.BadParameter("only GCP joining is supported for GCP auto-discovery") 90 } 91 92 if m.Params.JoinToken == "" { 93 m.Params.JoinToken = GCPInviteTokenName 94 } 95 96 if m.Params.ScriptName == "" { 97 m.Params.ScriptName = DefaultInstallerScriptName 98 } 99 } 100 101 if slices.Contains(m.Locations, Wildcard) || len(m.Locations) == 0 { 102 m.Locations = []string{Wildcard} 103 } 104 105 if slices.Contains(m.ProjectIDs, Wildcard) { 106 return trace.BadParameter("GCP discovery service project_ids does not support wildcards; please specify at least one value in project_ids.") 107 } 108 if len(m.ProjectIDs) == 0 { 109 return trace.BadParameter("GCP discovery service project_ids does cannot be empty; please specify at least one value in project_ids.") 110 } 111 112 if len(m.Labels) > 0 && len(m.Tags) > 0 { 113 return trace.BadParameter("labels and tags should not both be set.") 114 } 115 116 if len(m.Tags) > 0 { 117 m.Labels = m.Tags 118 } 119 120 if len(m.Labels) == 0 { 121 m.Labels = map[string]apiutils.Strings{ 122 Wildcard: {Wildcard}, 123 } 124 } 125 126 return nil 127 }