github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/types/matchers_azure.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  	// AzureInviteTokenName is the name of the default token to use
    29  	// when templating the script to be executed on Azure.
    30  	AzureInviteTokenName = "azure-discovery-token"
    31  
    32  	// AzureMatcherVM is the Azure matcher type for Azure VMs.
    33  	AzureMatcherVM = "vm"
    34  	// AzureMatcherKubernetes is the Azure matcher type for Azure Kubernetes.
    35  	AzureMatcherKubernetes = "aks"
    36  	// AzureMatcherMySQL is the Azure matcher type for Azure MySQL databases.
    37  	AzureMatcherMySQL = "mysql"
    38  	// AzureMatcherPostgres is the Azure matcher type for Azure Postgres databases.
    39  	AzureMatcherPostgres = "postgres"
    40  	// AzureMatcherRedis is the Azure matcher type for Azure Cache for Redis databases.
    41  	AzureMatcherRedis = "redis"
    42  	// AzureMatcherSQLServer is the Azure matcher type for SQL Server databases.
    43  	AzureMatcherSQLServer = "sqlserver"
    44  )
    45  
    46  // SupportedAzureMatchers is list of Azure services currently supported by the
    47  // Teleport discovery service.
    48  // IMPORTANT: when adding new Database matchers, make sure reference configs
    49  // for both Discovery and Database Service are updated in docs.
    50  var SupportedAzureMatchers = []string{
    51  	AzureMatcherVM,
    52  	AzureMatcherKubernetes,
    53  	AzureMatcherMySQL,
    54  	AzureMatcherPostgres,
    55  	AzureMatcherRedis,
    56  	AzureMatcherSQLServer,
    57  }
    58  
    59  // GetTypes gets the types that the matcher can match.
    60  func (m AzureMatcher) GetTypes() []string {
    61  	return m.Types
    62  }
    63  
    64  // CopyWithTypes copies the matcher with new types.
    65  func (m AzureMatcher) CopyWithTypes(t []string) Matcher {
    66  	newMatcher := m
    67  	newMatcher.Types = t
    68  	return newMatcher
    69  }
    70  
    71  // CheckAndSetDefaults that the matcher is correct and adds default values.
    72  func (m *AzureMatcher) CheckAndSetDefaults() error {
    73  	if len(m.Types) == 0 {
    74  		return trace.BadParameter("At least one Azure discovery service type must be specified, the supported resource types are: %v",
    75  			SupportedAzureMatchers)
    76  	}
    77  
    78  	for _, matcherType := range m.Types {
    79  		if !slices.Contains(SupportedAzureMatchers, matcherType) {
    80  			return trace.BadParameter("Azure discovery service type does not support %q resource type; supported resource types are: %v",
    81  				matcherType, SupportedAzureMatchers)
    82  		}
    83  	}
    84  
    85  	if slices.Contains(m.Types, AzureMatcherVM) {
    86  		if m.Params == nil {
    87  			m.Params = &InstallerParams{}
    88  		}
    89  		if m.Params.Azure == nil {
    90  			m.Params.Azure = &AzureInstallerParams{}
    91  		}
    92  
    93  		switch m.Params.JoinMethod {
    94  		case JoinMethodAzure, "":
    95  			m.Params.JoinMethod = JoinMethodAzure
    96  		default:
    97  			return trace.BadParameter("only Azure joining is supported for Azure auto-discovery")
    98  		}
    99  
   100  		if m.Params.JoinToken == "" {
   101  			m.Params.JoinToken = AzureInviteTokenName
   102  		}
   103  
   104  		if m.Params.ScriptName == "" {
   105  			m.Params.ScriptName = DefaultInstallerScriptName
   106  		}
   107  	}
   108  
   109  	if slices.Contains(m.Regions, Wildcard) || len(m.Regions) == 0 {
   110  		m.Regions = []string{Wildcard}
   111  	}
   112  
   113  	if slices.Contains(m.Subscriptions, Wildcard) || len(m.Subscriptions) == 0 {
   114  		m.Subscriptions = []string{Wildcard}
   115  	}
   116  
   117  	if slices.Contains(m.ResourceGroups, Wildcard) || len(m.ResourceGroups) == 0 {
   118  		m.ResourceGroups = []string{Wildcard}
   119  	}
   120  
   121  	if len(m.ResourceTags) == 0 {
   122  		m.ResourceTags = map[string]apiutils.Strings{
   123  			Wildcard: {Wildcard},
   124  		}
   125  	}
   126  	return nil
   127  }