config.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. package kubernetes
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "regexp"
  9. "strings"
  10. "time"
  11. "github.com/porter-dev/porter/internal/telemetry"
  12. "github.com/bufbuild/connect-go"
  13. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  14. "github.com/porter-dev/api-contracts/generated/go/porter/v1/porterv1connect"
  15. "github.com/porter-dev/porter/internal/models"
  16. "github.com/porter-dev/porter/internal/oauth"
  17. "github.com/porter-dev/porter/internal/repository"
  18. "golang.org/x/oauth2"
  19. "k8s.io/apimachinery/pkg/api/meta"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/cli-runtime/pkg/genericclioptions"
  22. "k8s.io/client-go/discovery"
  23. diskcached "k8s.io/client-go/discovery/cached/disk"
  24. "k8s.io/client-go/dynamic"
  25. "k8s.io/client-go/kubernetes"
  26. "k8s.io/client-go/kubernetes/fake"
  27. "k8s.io/client-go/rest"
  28. "k8s.io/client-go/restmapper"
  29. "k8s.io/client-go/tools/clientcmd"
  30. "k8s.io/client-go/tools/clientcmd/api"
  31. "k8s.io/client-go/util/homedir"
  32. ints "github.com/porter-dev/porter/internal/models/integrations"
  33. // this line will register plugins
  34. _ "k8s.io/client-go/plugin/pkg/client/auth"
  35. )
  36. // GetDynamicClientOutOfClusterConfig creates a new dynamic client using the OutOfClusterConfig
  37. func GetDynamicClientOutOfClusterConfig(conf *OutOfClusterConfig) (dynamic.Interface, error) {
  38. var restConf *rest.Config
  39. var err error
  40. if conf.AllowInClusterConnections && conf.Cluster.AuthMechanism == models.InCluster {
  41. restConf, err = rest.InClusterConfig()
  42. } else {
  43. restConf, err = conf.ToRESTConfig()
  44. }
  45. if err != nil {
  46. return nil, err
  47. }
  48. client, err := dynamic.NewForConfig(restConf)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return client, nil
  53. }
  54. // GetAgentOutOfClusterConfig creates a new Agent using the OutOfClusterConfig
  55. func GetAgentOutOfClusterConfig(ctx context.Context, conf *OutOfClusterConfig) (*Agent, error) {
  56. ctx, span := telemetry.NewSpan(ctx, "get-agent-out-of-cluster-config")
  57. defer span.End()
  58. if conf.AllowInClusterConnections && conf.Cluster.AuthMechanism == models.InCluster {
  59. return GetAgentInClusterConfig(ctx, conf.DefaultNamespace)
  60. }
  61. var restConf *rest.Config
  62. if conf.Cluster.ProvisionedBy == "CAPI" {
  63. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "provisioner", Value: conf.Cluster.ProvisionedBy})
  64. rc, err := restConfigForCAPICluster(ctx, conf.CAPIManagementClusterClient, *conf.Cluster)
  65. if err != nil {
  66. return nil, telemetry.Error(ctx, span, err, "error getting rest config for capi cluster")
  67. }
  68. restConf = rc
  69. } else {
  70. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "provisioner", Value: "non-capi"})
  71. rc, err := conf.ToRESTConfig()
  72. if err != nil {
  73. return nil, telemetry.Error(ctx, span, err, "error getting rest config")
  74. }
  75. restConf = rc
  76. }
  77. if restConf == nil {
  78. return nil, telemetry.Error(ctx, span, nil, "error getting rest config for cluster")
  79. }
  80. clientset, err := kubernetes.NewForConfig(restConf)
  81. if err != nil {
  82. return nil, telemetry.Error(ctx, span, err, "error getting new clientset for config")
  83. }
  84. agent := NewKubernetesAgent(ctx, conf, clientset)
  85. return &agent, nil
  86. }
  87. // restConfigForCAPICluster gets the kubernetes rest API client for a CAPI cluster
  88. func restConfigForCAPICluster(ctx context.Context, mgmtClusterConnection porterv1connect.ClusterControlPlaneServiceClient, cluster models.Cluster) (*rest.Config, error) {
  89. ctx, span := telemetry.NewSpan(ctx, "rest-config-for-capi-cluster")
  90. defer span.End()
  91. kc, err := kubeConfigForCAPICluster(ctx, mgmtClusterConnection, cluster)
  92. if err != nil {
  93. return nil, telemetry.Error(ctx, span, err, "error getting kubeconfig")
  94. }
  95. rc, err := writeKubeConfigToFileAndRestClient([]byte(kc))
  96. if err != nil {
  97. return nil, telemetry.Error(ctx, span, err, "error writing kubeconfig to file")
  98. }
  99. return rc, nil
  100. }
  101. // kubeConfigForCAPICluster grabs the raw kube config for a capi cluster
  102. func kubeConfigForCAPICluster(ctx context.Context, mgmtClusterConnection porterv1connect.ClusterControlPlaneServiceClient, cluster models.Cluster) (string, error) {
  103. ctx, span := telemetry.NewSpan(ctx, "kubeconfig-capi")
  104. defer span.End()
  105. if cluster.ProjectID == 0 {
  106. return "", telemetry.Error(ctx, span, nil, "missing project id")
  107. }
  108. if cluster.ID == 0 {
  109. return "", telemetry.Error(ctx, span, nil, "missing cluster id")
  110. }
  111. telemetry.WithAttributes(span,
  112. telemetry.AttributeKV{Key: "project-id", Value: cluster.ProjectID},
  113. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  114. )
  115. kubeconfigResp, err := mgmtClusterConnection.KubeConfigForCluster(ctx, connect.NewRequest(
  116. &porterv1.KubeConfigForClusterRequest{
  117. ProjectId: int64(cluster.ProjectID),
  118. ClusterId: int64(cluster.ID),
  119. },
  120. ))
  121. if err != nil {
  122. return "", telemetry.Error(ctx, span, err, "error getting capi config")
  123. }
  124. if kubeconfigResp.Msg == nil {
  125. return "", telemetry.Error(ctx, span, nil, "no msg returned for capi cluster")
  126. }
  127. if kubeconfigResp.Msg.KubeConfig == "" {
  128. return "", telemetry.Error(ctx, span, nil, "no kubeconfig returned for capi cluster")
  129. }
  130. decodedKubeconfig, err := base64.StdEncoding.DecodeString(kubeconfigResp.Msg.KubeConfig)
  131. if err != nil {
  132. return "", telemetry.Error(ctx, span, nil, "error decoding capi cluster")
  133. }
  134. return string(decodedKubeconfig), nil
  135. }
  136. // writeKubeConfigToFileAndRestClient writes a literal kubeconfig to a temporary file
  137. // then uses the client-go kubernetes package to create a rest.Config from it
  138. func writeKubeConfigToFileAndRestClient(kubeconf []byte) (*rest.Config, error) {
  139. tmpFile, err := os.CreateTemp(os.TempDir(), "kconf-")
  140. if err != nil {
  141. return nil, fmt.Errorf("unable to create temp file: %w", err)
  142. }
  143. defer os.Remove(tmpFile.Name())
  144. if _, err = tmpFile.Write(kubeconf); err != nil {
  145. return nil, fmt.Errorf("unable to write to temp file: %w", err)
  146. }
  147. if err := tmpFile.Close(); err != nil {
  148. return nil, fmt.Errorf("unable to close temp file: %w", err)
  149. }
  150. kconfPath, err := filepath.Abs(tmpFile.Name())
  151. if err != nil {
  152. return nil, fmt.Errorf("unable to find temp file: %w", err)
  153. }
  154. rest, err := clientcmd.BuildConfigFromFlags("", kconfPath)
  155. if err != nil {
  156. return nil, fmt.Errorf("unable create rest config from temp file: %w", err)
  157. }
  158. return rest, nil
  159. }
  160. // IsInCluster returns true if the process is running in a Kubernetes cluster,
  161. // false otherwise
  162. func IsInCluster() bool {
  163. _, err := rest.InClusterConfig()
  164. // If the error is not nil, it is either rest.ErrNotInCluster or the in-cluster
  165. // config cannot be read. In either case, in-cluster operations are not supported.
  166. return err == nil
  167. }
  168. // GetAgentInClusterConfig uses the service account that kubernetes
  169. // gives to pods to connect
  170. func GetAgentInClusterConfig(ctx context.Context, namespace string) (*Agent, error) {
  171. conf, err := rest.InClusterConfig()
  172. if err != nil {
  173. return nil, fmt.Errorf("error getting in cluster config: %w", err)
  174. }
  175. restClientGetter := NewRESTClientGetterFromInClusterConfig(conf, namespace)
  176. clientset, err := kubernetes.NewForConfig(conf)
  177. if err != nil {
  178. return nil, fmt.Errorf("error getting new clientset for config: %w", err)
  179. }
  180. agent := NewKubernetesAgent(ctx, restClientGetter, clientset)
  181. return &agent, nil
  182. }
  183. // GetAgentTesting creates a new Agent using an optional existing storage class
  184. // TODO: this should be in a test package, not here.
  185. func GetAgentTesting(objects ...runtime.Object) *Agent {
  186. agent := NewKubernetesAgent(context.Background(), &fakeRESTClientGetter{}, fake.NewSimpleClientset(objects...))
  187. return &agent
  188. }
  189. // OutOfClusterConfig is the set of parameters required for an out-of-cluster connection.
  190. // This implements RESTClientGetter
  191. type OutOfClusterConfig struct {
  192. Cluster *models.Cluster
  193. Repo repository.Repository
  194. DefaultNamespace string // optional
  195. AllowInClusterConnections bool
  196. Timeout time.Duration // optional
  197. // Only required if using DigitalOcean OAuth as an auth mechanism
  198. DigitalOceanOAuth *oauth2.Config
  199. CAPIManagementClusterClient porterv1connect.ClusterControlPlaneServiceClient
  200. }
  201. // ToRESTConfig creates a kubernetes REST client factory -- it calls ClientConfig on
  202. // the result of ToRawKubeConfigLoader, and also adds a custom http transport layer
  203. // if necessary (required for GCP auth).
  204. // TODO: this should be split out from OutOfClusterConfig, and implemented separately in order to wrap the kubernetes RESTGetter interface.
  205. // Until then, we lose context propagation on all these calls
  206. func (conf *OutOfClusterConfig) ToRESTConfig() (*rest.Config, error) {
  207. ctx, span := telemetry.NewSpan(context.Background(), "ooc-to-rest-config")
  208. defer span.End()
  209. telemetry.WithAttributes(span,
  210. telemetry.AttributeKV{Key: "cluster-id", Value: conf.Cluster.ID},
  211. telemetry.AttributeKV{Key: "project-id", Value: conf.Cluster.ProjectID},
  212. )
  213. if conf.Cluster.ProvisionedBy == "CAPI" {
  214. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "capi-provisioned", Value: true})
  215. rc, err := restConfigForCAPICluster(ctx, conf.CAPIManagementClusterClient, *conf.Cluster)
  216. if err != nil {
  217. return nil, telemetry.Error(ctx, span, err, "error getting config for capi cluster")
  218. }
  219. return rc, nil
  220. }
  221. cmdConf, err := conf.GetClientConfigFromCluster(ctx)
  222. if err != nil {
  223. return nil, telemetry.Error(ctx, span, err, "error getting client config from cluster")
  224. }
  225. restConf, err := cmdConf.ClientConfig()
  226. if err != nil {
  227. return nil, telemetry.Error(ctx, span, err, "error getting client config")
  228. }
  229. restConf.Timeout = conf.Timeout
  230. rest.SetKubernetesDefaults(restConf)
  231. return restConf, nil
  232. }
  233. // ToRawKubeConfigLoader creates a clientcmd.ClientConfig from the raw kubeconfig found in
  234. // the OutOfClusterConfig. It does not implement loading rules or overrides.
  235. func (conf *OutOfClusterConfig) ToRawKubeConfigLoader() clientcmd.ClientConfig {
  236. ctx, span := telemetry.NewSpan(context.Background(), "ooc-to-raw-kubeconfig-loader")
  237. defer span.End()
  238. cmdConf, _ := conf.GetClientConfigFromCluster(ctx)
  239. return cmdConf
  240. }
  241. // ToDiscoveryClient returns a CachedDiscoveryInterface using a computed RESTConfig
  242. // It's required to implement the interface genericclioptions.RESTClientGetter
  243. func (conf *OutOfClusterConfig) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
  244. // From: k8s.io/cli-runtime/pkg/genericclioptions/config_flags.go > func (*configFlags) ToDiscoveryClient()
  245. restConf, err := conf.ToRESTConfig()
  246. if err != nil {
  247. return nil, err
  248. }
  249. restConf.Burst = 100
  250. defaultHTTPCacheDir := filepath.Join(homedir.HomeDir(), ".kube", "http-cache")
  251. // takes the parentDir and the host and comes up with a "usually non-colliding" name for the discoveryCacheDir
  252. parentDir := filepath.Join(homedir.HomeDir(), ".kube", "cache", "discovery")
  253. // strip the optional scheme from host if its there:
  254. schemelessHost := strings.Replace(strings.Replace(restConf.Host, "https://", "", 1), "http://", "", 1)
  255. // now do a simple collapse of non-AZ09 characters. Collisions are possible but unlikely. Even if we do collide the problem is short lived
  256. safeHost := regexp.MustCompile(`[^(\w/\.)]`).ReplaceAllString(schemelessHost, "_")
  257. discoveryCacheDir := filepath.Join(parentDir, safeHost)
  258. return diskcached.NewCachedDiscoveryClientForConfig(restConf, discoveryCacheDir, defaultHTTPCacheDir, time.Duration(10*time.Minute))
  259. }
  260. // ToRESTMapper returns a mapper
  261. func (conf *OutOfClusterConfig) ToRESTMapper() (meta.RESTMapper, error) {
  262. // From: k8s.io/cli-runtime/pkg/genericclioptions/config_flags.go > func (*configFlags) ToRESTMapper()
  263. discoveryClient, err := conf.ToDiscoveryClient()
  264. if err != nil {
  265. return nil, err
  266. }
  267. mapper := restmapper.NewDeferredDiscoveryRESTMapper(discoveryClient)
  268. expander := restmapper.NewShortcutExpander(mapper, discoveryClient)
  269. return expander, nil
  270. }
  271. // GetClientConfigFromCluster will construct new clientcmd.ClientConfig using
  272. // the configuration saved within a Cluster model
  273. func (conf *OutOfClusterConfig) GetClientConfigFromCluster(ctx context.Context) (clientcmd.ClientConfig, error) {
  274. ctx, span := telemetry.NewSpan(ctx, "ooc-get-client-config-from-cluster")
  275. defer span.End()
  276. if conf.Cluster == nil {
  277. return nil, telemetry.Error(ctx, span, nil, "cluster cannot be nil")
  278. }
  279. if conf.Cluster.ProvisionedBy == "CAPI" {
  280. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "capi-provisioned", Value: true})
  281. rc, err := kubeConfigForCAPICluster(ctx, conf.CAPIManagementClusterClient, *conf.Cluster)
  282. if err != nil {
  283. return nil, telemetry.Error(ctx, span, err, "error getting capi kube config")
  284. }
  285. clientConfig, err := clientcmd.NewClientConfigFromBytes([]byte(rc))
  286. if err != nil {
  287. return nil, telemetry.Error(ctx, span, err, "error getting config from bytes")
  288. }
  289. rawConfig, err := clientConfig.RawConfig()
  290. if err != nil {
  291. return nil, telemetry.Error(ctx, span, err, "error getting raw config")
  292. }
  293. overrides := &clientcmd.ConfigOverrides{}
  294. if conf.DefaultNamespace != "" {
  295. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "namespace-override", Value: conf.DefaultNamespace})
  296. overrides.Context = api.Context{
  297. Namespace: conf.DefaultNamespace,
  298. }
  299. }
  300. return clientcmd.NewDefaultClientConfig(rawConfig, overrides), nil
  301. }
  302. if conf.Cluster.AuthMechanism == models.Local {
  303. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "local-provisioned", Value: true})
  304. kubeAuth, err := conf.Repo.KubeIntegration().ReadKubeIntegration(
  305. conf.Cluster.ProjectID,
  306. conf.Cluster.KubeIntegrationID,
  307. )
  308. if err != nil {
  309. return nil, telemetry.Error(ctx, span, err, "error reading kube integration")
  310. }
  311. return clientcmd.NewClientConfigFromBytes(kubeAuth.Kubeconfig)
  312. }
  313. apiConfig, err := conf.CreateRawConfigFromCluster(ctx)
  314. if err != nil {
  315. return nil, telemetry.Error(ctx, span, err, "error creating raw config from cluster")
  316. }
  317. overrides := &clientcmd.ConfigOverrides{}
  318. if conf.DefaultNamespace != "" {
  319. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "namespace-override", Value: conf.DefaultNamespace})
  320. overrides.Context = api.Context{
  321. Namespace: conf.DefaultNamespace,
  322. }
  323. }
  324. config := clientcmd.NewDefaultClientConfig(*apiConfig, overrides)
  325. return config, nil
  326. }
  327. func (conf *OutOfClusterConfig) CreateRawConfigFromCluster(ctx context.Context) (*api.Config, error) {
  328. ctx, span := telemetry.NewSpan(ctx, "ooc-create-raw-config-from-cluster")
  329. defer span.End()
  330. cluster := conf.Cluster
  331. apiConfig := &api.Config{}
  332. clusterMap := make(map[string]*api.Cluster)
  333. clusterMap[cluster.Name] = &api.Cluster{
  334. Server: cluster.Server,
  335. LocationOfOrigin: cluster.ClusterLocationOfOrigin,
  336. TLSServerName: cluster.TLSServerName,
  337. InsecureSkipTLSVerify: cluster.InsecureSkipTLSVerify,
  338. CertificateAuthorityData: cluster.CertificateAuthorityData,
  339. }
  340. // construct the auth infos
  341. authInfoName := cluster.Name + "-" + string(cluster.AuthMechanism)
  342. authInfoMap := make(map[string]*api.AuthInfo)
  343. authInfoMap[authInfoName] = &api.AuthInfo{
  344. LocationOfOrigin: cluster.UserLocationOfOrigin,
  345. Impersonate: cluster.UserImpersonate,
  346. }
  347. if groups := strings.Split(cluster.UserImpersonateGroups, ","); len(groups) > 0 && groups[0] != "" {
  348. authInfoMap[authInfoName].ImpersonateGroups = groups
  349. }
  350. telemetry.WithAttributes(span,
  351. telemetry.AttributeKV{Key: "auth-mechanism", Value: cluster.AuthMechanism},
  352. telemetry.AttributeKV{Key: "server", Value: cluster.Server},
  353. )
  354. switch cluster.AuthMechanism {
  355. case models.X509:
  356. kubeAuth, err := conf.Repo.KubeIntegration().ReadKubeIntegration(
  357. cluster.ProjectID,
  358. cluster.KubeIntegrationID,
  359. )
  360. if err != nil {
  361. return nil, telemetry.Error(ctx, span, err, "error reading kube integration")
  362. }
  363. telemetry.WithAttributes(span,
  364. telemetry.AttributeKV{Key: "integration-id", Value: cluster.KubeIntegrationID},
  365. )
  366. authInfoMap[authInfoName].ClientCertificateData = kubeAuth.ClientCertificateData
  367. authInfoMap[authInfoName].ClientKeyData = kubeAuth.ClientKeyData
  368. case models.Basic:
  369. kubeAuth, err := conf.Repo.KubeIntegration().ReadKubeIntegration(
  370. cluster.ProjectID,
  371. cluster.KubeIntegrationID,
  372. )
  373. if err != nil {
  374. return nil, telemetry.Error(ctx, span, err, "error reading kube integration")
  375. }
  376. telemetry.WithAttributes(span,
  377. telemetry.AttributeKV{Key: "integration-id", Value: cluster.KubeIntegrationID},
  378. )
  379. authInfoMap[authInfoName].Username = string(kubeAuth.Username)
  380. authInfoMap[authInfoName].Password = string(kubeAuth.Password)
  381. case models.Bearer:
  382. kubeAuth, err := conf.Repo.KubeIntegration().ReadKubeIntegration(
  383. cluster.ProjectID,
  384. cluster.KubeIntegrationID,
  385. )
  386. if err != nil {
  387. return nil, telemetry.Error(ctx, span, err, "error reading kube integration")
  388. }
  389. authInfoMap[authInfoName].Token = string(kubeAuth.Token)
  390. case models.OIDC:
  391. oidcAuth, err := conf.Repo.OIDCIntegration().ReadOIDCIntegration(
  392. cluster.ProjectID,
  393. cluster.OIDCIntegrationID,
  394. )
  395. if err != nil {
  396. return nil, telemetry.Error(ctx, span, err, "error reading oidc integration")
  397. }
  398. telemetry.WithAttributes(span,
  399. telemetry.AttributeKV{Key: "integration-id", Value: cluster.OIDCIntegrationID},
  400. )
  401. authInfoMap[authInfoName].AuthProvider = &api.AuthProviderConfig{
  402. Name: "oidc",
  403. Config: map[string]string{
  404. "idp-issuer-url": string(oidcAuth.IssuerURL),
  405. "client-id": string(oidcAuth.ClientID),
  406. "client-secret": string(oidcAuth.ClientSecret),
  407. "idp-certificate-authority-data": string(oidcAuth.CertificateAuthorityData),
  408. "id-token": string(oidcAuth.IDToken),
  409. "refresh-token": string(oidcAuth.RefreshToken),
  410. },
  411. }
  412. case models.GCP:
  413. gcpAuth, err := conf.Repo.GCPIntegration().ReadGCPIntegration(
  414. cluster.ProjectID,
  415. cluster.GCPIntegrationID,
  416. )
  417. if err != nil {
  418. return nil, telemetry.Error(ctx, span, err, "error reading gcp integration")
  419. }
  420. telemetry.WithAttributes(span,
  421. telemetry.AttributeKV{Key: "integration-id", Value: cluster.GCPIntegrationID},
  422. )
  423. tok, err := gcpAuth.GetBearerToken(
  424. ctx,
  425. conf.getTokenCache,
  426. conf.setTokenCache,
  427. "https://www.googleapis.com/auth/cloud-platform",
  428. )
  429. if err != nil {
  430. return nil, telemetry.Error(ctx, span, err, "error getting gcp token")
  431. }
  432. if tok == nil {
  433. return nil, telemetry.Error(ctx, span, nil, "unable to get gcp token")
  434. }
  435. // add this as a bearer token
  436. authInfoMap[authInfoName].Token = tok.AccessToken
  437. case models.AWS:
  438. awsAuth, err := conf.Repo.AWSIntegration().ReadAWSIntegration(
  439. cluster.ProjectID,
  440. cluster.AWSIntegrationID,
  441. )
  442. if err != nil {
  443. return nil, telemetry.Error(ctx, span, err, "error reading aws integration")
  444. }
  445. telemetry.WithAttributes(span,
  446. telemetry.AttributeKV{Key: "integration-id", Value: cluster.AWSIntegrationID},
  447. )
  448. awsClusterID := cluster.Name
  449. shouldOverride := false
  450. if cluster.AWSClusterID != "" {
  451. awsClusterID = cluster.AWSClusterID
  452. shouldOverride = true
  453. }
  454. tok, err := awsAuth.GetBearerToken(ctx, conf.getTokenCache, conf.setTokenCache, awsClusterID, shouldOverride)
  455. if err != nil {
  456. return nil, telemetry.Error(ctx, span, err, "unable to get AWS bearer token")
  457. }
  458. // add this as a bearer token
  459. authInfoMap[authInfoName].Token = tok
  460. case models.DO:
  461. oauthInt, err := conf.Repo.OAuthIntegration().ReadOAuthIntegration(
  462. cluster.ProjectID,
  463. cluster.DOIntegrationID,
  464. )
  465. if err != nil {
  466. return nil, telemetry.Error(ctx, span, err, "error reading oauth integration")
  467. }
  468. telemetry.WithAttributes(span,
  469. telemetry.AttributeKV{Key: "integration-id", Value: cluster.DOIntegrationID},
  470. )
  471. tok, _, err := oauth.GetAccessToken(oauthInt.SharedOAuthModel, conf.DigitalOceanOAuth, oauth.MakeUpdateOAuthIntegrationTokenFunction(oauthInt, conf.Repo))
  472. if err != nil {
  473. return nil, telemetry.Error(ctx, span, err, "unable to get oauth access token for Digital Ocean")
  474. }
  475. // add this as a bearer token
  476. authInfoMap[authInfoName].Token = tok
  477. case models.Azure:
  478. azInt, err := conf.Repo.AzureIntegration().ReadAzureIntegration(
  479. cluster.ProjectID,
  480. cluster.AzureIntegrationID,
  481. )
  482. if err != nil {
  483. return nil, telemetry.Error(ctx, span, err, "error reading azure integration")
  484. }
  485. telemetry.WithAttributes(span,
  486. telemetry.AttributeKV{Key: "integration-id", Value: cluster.AzureIntegrationID},
  487. )
  488. authInfoMap[authInfoName].Token = string(azInt.AKSPassword)
  489. default:
  490. return nil, telemetry.Error(ctx, span, nil, "auth mechanism not supported")
  491. }
  492. // create a context of the cluster name
  493. contextMap := make(map[string]*api.Context)
  494. contextMap[cluster.Name] = &api.Context{
  495. LocationOfOrigin: cluster.ClusterLocationOfOrigin,
  496. Cluster: cluster.Name,
  497. AuthInfo: authInfoName,
  498. }
  499. apiConfig.Clusters = clusterMap
  500. apiConfig.AuthInfos = authInfoMap
  501. apiConfig.Contexts = contextMap
  502. apiConfig.CurrentContext = cluster.Name
  503. return apiConfig, nil
  504. }
  505. func (conf *OutOfClusterConfig) getTokenCache(ctx context.Context) (tok *ints.TokenCache, err error) {
  506. return &conf.Cluster.TokenCache.TokenCache, nil
  507. }
  508. func (conf *OutOfClusterConfig) setTokenCache(ctx context.Context, token string, expiry time.Time) error {
  509. _, err := conf.Repo.Cluster().UpdateClusterTokenCache(
  510. &ints.ClusterTokenCache{
  511. ClusterID: conf.Cluster.ID,
  512. TokenCache: ints.TokenCache{
  513. Token: []byte(token),
  514. Expiry: expiry,
  515. },
  516. },
  517. )
  518. return err
  519. }
  520. // NewRESTClientGetterFromInClusterConfig returns a RESTClientGetter using
  521. // default values set from the *rest.Config
  522. func NewRESTClientGetterFromInClusterConfig(conf *rest.Config, namespace string) genericclioptions.RESTClientGetter {
  523. cfs := genericclioptions.NewConfigFlags(false)
  524. if namespace != "" {
  525. cfs.Namespace = &namespace
  526. }
  527. cfs.ClusterName = &conf.ServerName
  528. cfs.Insecure = &conf.Insecure
  529. cfs.APIServer = &conf.Host
  530. cfs.CAFile = &conf.CAFile
  531. cfs.KeyFile = &conf.KeyFile
  532. cfs.CertFile = &conf.CertFile
  533. cfs.BearerToken = &conf.BearerToken
  534. cfs.Timeout = stringptr(conf.Timeout.String())
  535. cfs.Impersonate = &conf.Impersonate.UserName
  536. cfs.ImpersonateGroup = &conf.Impersonate.Groups
  537. cfs.Username = &conf.Username
  538. cfs.Password = &conf.Password
  539. return cfs
  540. }
  541. func stringptr(val string) *string {
  542. return &val
  543. }
  544. type fakeRESTClientGetter struct{}
  545. func (f *fakeRESTClientGetter) ToRESTConfig() (*rest.Config, error) {
  546. return nil, nil
  547. }
  548. func (f *fakeRESTClientGetter) ToRawKubeConfigLoader() clientcmd.ClientConfig {
  549. return nil
  550. }
  551. func (f *fakeRESTClientGetter) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
  552. return nil, nil
  553. }
  554. func (f *fakeRESTClientGetter) ToRESTMapper() (meta.RESTMapper, error) {
  555. return nil, nil
  556. }