config.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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 := context.Background()
  208. // ctx, span := telemetry.NewSpan(context.Background(), "ooc-to-rest-config")
  209. // defer span.End()
  210. // telemetry.WithAttributes(span,
  211. // telemetry.AttributeKV{Key: "cluster-id", Value: conf.Cluster.ID},
  212. // telemetry.AttributeKV{Key: "project-id", Value: conf.Cluster.ProjectID},
  213. // )
  214. if conf.Cluster.ProvisionedBy == "CAPI" {
  215. // telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "capi-provisioned", Value: true})
  216. rc, err := restConfigForCAPICluster(ctx, conf.CAPIManagementClusterClient, *conf.Cluster)
  217. if err != nil {
  218. return nil, fmt.Errorf("error getting config for capi cluster: %w", err)
  219. }
  220. return rc, nil
  221. }
  222. cmdConf, err := conf.GetClientConfigFromCluster(ctx)
  223. if err != nil {
  224. return nil, fmt.Errorf("error getting client config from cluster: %w", err)
  225. }
  226. restConf, err := cmdConf.ClientConfig()
  227. if err != nil {
  228. return nil, fmt.Errorf("error getting client config: %w", err)
  229. }
  230. restConf.Timeout = conf.Timeout
  231. rest.SetKubernetesDefaults(restConf)
  232. return restConf, nil
  233. }
  234. // ToRawKubeConfigLoader creates a clientcmd.ClientConfig from the raw kubeconfig found in
  235. // the OutOfClusterConfig. It does not implement loading rules or overrides.
  236. func (conf *OutOfClusterConfig) ToRawKubeConfigLoader() clientcmd.ClientConfig {
  237. ctx, span := telemetry.NewSpan(context.Background(), "ooc-to-raw-kubeconfig-loader")
  238. defer span.End()
  239. cmdConf, _ := conf.GetClientConfigFromCluster(ctx)
  240. return cmdConf
  241. }
  242. // ToDiscoveryClient returns a CachedDiscoveryInterface using a computed RESTConfig
  243. // It's required to implement the interface genericclioptions.RESTClientGetter
  244. func (conf *OutOfClusterConfig) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
  245. // From: k8s.io/cli-runtime/pkg/genericclioptions/config_flags.go > func (*configFlags) ToDiscoveryClient()
  246. restConf, err := conf.ToRESTConfig()
  247. if err != nil {
  248. return nil, err
  249. }
  250. restConf.Burst = 100
  251. defaultHTTPCacheDir := filepath.Join(homedir.HomeDir(), ".kube", "http-cache")
  252. // takes the parentDir and the host and comes up with a "usually non-colliding" name for the discoveryCacheDir
  253. parentDir := filepath.Join(homedir.HomeDir(), ".kube", "cache", "discovery")
  254. // strip the optional scheme from host if its there:
  255. schemelessHost := strings.Replace(strings.Replace(restConf.Host, "https://", "", 1), "http://", "", 1)
  256. // now do a simple collapse of non-AZ09 characters. Collisions are possible but unlikely. Even if we do collide the problem is short lived
  257. safeHost := regexp.MustCompile(`[^(\w/\.)]`).ReplaceAllString(schemelessHost, "_")
  258. discoveryCacheDir := filepath.Join(parentDir, safeHost)
  259. return diskcached.NewCachedDiscoveryClientForConfig(restConf, discoveryCacheDir, defaultHTTPCacheDir, time.Duration(10*time.Minute))
  260. }
  261. // ToRESTMapper returns a mapper
  262. func (conf *OutOfClusterConfig) ToRESTMapper() (meta.RESTMapper, error) {
  263. // From: k8s.io/cli-runtime/pkg/genericclioptions/config_flags.go > func (*configFlags) ToRESTMapper()
  264. discoveryClient, err := conf.ToDiscoveryClient()
  265. if err != nil {
  266. return nil, err
  267. }
  268. mapper := restmapper.NewDeferredDiscoveryRESTMapper(discoveryClient)
  269. expander := restmapper.NewShortcutExpander(mapper, discoveryClient)
  270. return expander, nil
  271. }
  272. // GetClientConfigFromCluster will construct new clientcmd.ClientConfig using
  273. // the configuration saved within a Cluster model
  274. func (conf *OutOfClusterConfig) GetClientConfigFromCluster(ctx context.Context) (clientcmd.ClientConfig, error) {
  275. ctx, span := telemetry.NewSpan(ctx, "ooc-get-client-config-from-cluster")
  276. defer span.End()
  277. if conf.Cluster == nil {
  278. return nil, telemetry.Error(ctx, span, nil, "cluster cannot be nil")
  279. }
  280. if conf.Cluster.ProvisionedBy == "CAPI" {
  281. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "capi-provisioned", Value: true})
  282. rc, err := kubeConfigForCAPICluster(ctx, conf.CAPIManagementClusterClient, *conf.Cluster)
  283. if err != nil {
  284. return nil, telemetry.Error(ctx, span, err, "error getting capi kube config")
  285. }
  286. clientConfig, err := clientcmd.NewClientConfigFromBytes([]byte(rc))
  287. if err != nil {
  288. return nil, telemetry.Error(ctx, span, err, "error getting config from bytes")
  289. }
  290. rawConfig, err := clientConfig.RawConfig()
  291. if err != nil {
  292. return nil, telemetry.Error(ctx, span, err, "error getting raw config")
  293. }
  294. overrides := &clientcmd.ConfigOverrides{}
  295. if conf.DefaultNamespace != "" {
  296. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "namespace-override", Value: conf.DefaultNamespace})
  297. overrides.Context = api.Context{
  298. Namespace: conf.DefaultNamespace,
  299. }
  300. }
  301. return clientcmd.NewDefaultClientConfig(rawConfig, overrides), nil
  302. }
  303. if conf.Cluster.AuthMechanism == models.Local {
  304. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "local-provisioned", Value: true})
  305. kubeAuth, err := conf.Repo.KubeIntegration().ReadKubeIntegration(
  306. conf.Cluster.ProjectID,
  307. conf.Cluster.KubeIntegrationID,
  308. )
  309. if err != nil {
  310. return nil, telemetry.Error(ctx, span, err, "error reading kube integration")
  311. }
  312. return clientcmd.NewClientConfigFromBytes(kubeAuth.Kubeconfig)
  313. }
  314. apiConfig, err := conf.CreateRawConfigFromCluster(ctx)
  315. if err != nil {
  316. return nil, telemetry.Error(ctx, span, err, "error creating raw config from cluster")
  317. }
  318. overrides := &clientcmd.ConfigOverrides{}
  319. if conf.DefaultNamespace != "" {
  320. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "namespace-override", Value: conf.DefaultNamespace})
  321. overrides.Context = api.Context{
  322. Namespace: conf.DefaultNamespace,
  323. }
  324. }
  325. config := clientcmd.NewDefaultClientConfig(*apiConfig, overrides)
  326. return config, nil
  327. }
  328. func (conf *OutOfClusterConfig) CreateRawConfigFromCluster(ctx context.Context) (*api.Config, error) {
  329. ctx, span := telemetry.NewSpan(ctx, "ooc-create-raw-config-from-cluster")
  330. defer span.End()
  331. cluster := conf.Cluster
  332. apiConfig := &api.Config{}
  333. clusterMap := make(map[string]*api.Cluster)
  334. clusterMap[cluster.Name] = &api.Cluster{
  335. Server: cluster.Server,
  336. LocationOfOrigin: cluster.ClusterLocationOfOrigin,
  337. TLSServerName: cluster.TLSServerName,
  338. InsecureSkipTLSVerify: cluster.InsecureSkipTLSVerify,
  339. CertificateAuthorityData: cluster.CertificateAuthorityData,
  340. }
  341. // construct the auth infos
  342. authInfoName := cluster.Name + "-" + string(cluster.AuthMechanism)
  343. authInfoMap := make(map[string]*api.AuthInfo)
  344. authInfoMap[authInfoName] = &api.AuthInfo{
  345. LocationOfOrigin: cluster.UserLocationOfOrigin,
  346. Impersonate: cluster.UserImpersonate,
  347. }
  348. if groups := strings.Split(cluster.UserImpersonateGroups, ","); len(groups) > 0 && groups[0] != "" {
  349. authInfoMap[authInfoName].ImpersonateGroups = groups
  350. }
  351. telemetry.WithAttributes(span,
  352. telemetry.AttributeKV{Key: "auth-mechanism", Value: cluster.AuthMechanism},
  353. telemetry.AttributeKV{Key: "server", Value: cluster.Server},
  354. )
  355. switch cluster.AuthMechanism {
  356. case models.X509:
  357. kubeAuth, err := conf.Repo.KubeIntegration().ReadKubeIntegration(
  358. cluster.ProjectID,
  359. cluster.KubeIntegrationID,
  360. )
  361. if err != nil {
  362. return nil, telemetry.Error(ctx, span, err, "error reading kube integration")
  363. }
  364. telemetry.WithAttributes(span,
  365. telemetry.AttributeKV{Key: "integration-id", Value: cluster.KubeIntegrationID},
  366. )
  367. authInfoMap[authInfoName].ClientCertificateData = kubeAuth.ClientCertificateData
  368. authInfoMap[authInfoName].ClientKeyData = kubeAuth.ClientKeyData
  369. case models.Basic:
  370. kubeAuth, err := conf.Repo.KubeIntegration().ReadKubeIntegration(
  371. cluster.ProjectID,
  372. cluster.KubeIntegrationID,
  373. )
  374. if err != nil {
  375. return nil, telemetry.Error(ctx, span, err, "error reading kube integration")
  376. }
  377. telemetry.WithAttributes(span,
  378. telemetry.AttributeKV{Key: "integration-id", Value: cluster.KubeIntegrationID},
  379. )
  380. authInfoMap[authInfoName].Username = string(kubeAuth.Username)
  381. authInfoMap[authInfoName].Password = string(kubeAuth.Password)
  382. case models.Bearer:
  383. kubeAuth, err := conf.Repo.KubeIntegration().ReadKubeIntegration(
  384. cluster.ProjectID,
  385. cluster.KubeIntegrationID,
  386. )
  387. if err != nil {
  388. return nil, telemetry.Error(ctx, span, err, "error reading kube integration")
  389. }
  390. authInfoMap[authInfoName].Token = string(kubeAuth.Token)
  391. case models.OIDC:
  392. oidcAuth, err := conf.Repo.OIDCIntegration().ReadOIDCIntegration(
  393. cluster.ProjectID,
  394. cluster.OIDCIntegrationID,
  395. )
  396. if err != nil {
  397. return nil, telemetry.Error(ctx, span, err, "error reading oidc integration")
  398. }
  399. telemetry.WithAttributes(span,
  400. telemetry.AttributeKV{Key: "integration-id", Value: cluster.OIDCIntegrationID},
  401. )
  402. authInfoMap[authInfoName].AuthProvider = &api.AuthProviderConfig{
  403. Name: "oidc",
  404. Config: map[string]string{
  405. "idp-issuer-url": string(oidcAuth.IssuerURL),
  406. "client-id": string(oidcAuth.ClientID),
  407. "client-secret": string(oidcAuth.ClientSecret),
  408. "idp-certificate-authority-data": string(oidcAuth.CertificateAuthorityData),
  409. "id-token": string(oidcAuth.IDToken),
  410. "refresh-token": string(oidcAuth.RefreshToken),
  411. },
  412. }
  413. case models.GCP:
  414. gcpAuth, err := conf.Repo.GCPIntegration().ReadGCPIntegration(
  415. cluster.ProjectID,
  416. cluster.GCPIntegrationID,
  417. )
  418. if err != nil {
  419. return nil, telemetry.Error(ctx, span, err, "error reading gcp integration")
  420. }
  421. telemetry.WithAttributes(span,
  422. telemetry.AttributeKV{Key: "integration-id", Value: cluster.GCPIntegrationID},
  423. )
  424. tok, err := gcpAuth.GetBearerToken(
  425. ctx,
  426. conf.getTokenCache,
  427. conf.setTokenCache,
  428. "https://www.googleapis.com/auth/cloud-platform",
  429. )
  430. if err != nil {
  431. return nil, telemetry.Error(ctx, span, err, "error getting gcp token")
  432. }
  433. if tok == nil {
  434. return nil, telemetry.Error(ctx, span, nil, "unable to get gcp token")
  435. }
  436. // add this as a bearer token
  437. authInfoMap[authInfoName].Token = tok.AccessToken
  438. case models.AWS:
  439. awsAuth, err := conf.Repo.AWSIntegration().ReadAWSIntegration(
  440. cluster.ProjectID,
  441. cluster.AWSIntegrationID,
  442. )
  443. if err != nil {
  444. return nil, telemetry.Error(ctx, span, err, "error reading aws integration")
  445. }
  446. telemetry.WithAttributes(span,
  447. telemetry.AttributeKV{Key: "integration-id", Value: cluster.AWSIntegrationID},
  448. )
  449. awsClusterID := cluster.Name
  450. shouldOverride := false
  451. if cluster.AWSClusterID != "" {
  452. awsClusterID = cluster.AWSClusterID
  453. shouldOverride = true
  454. }
  455. tok, err := awsAuth.GetBearerToken(ctx, conf.getTokenCache, conf.setTokenCache, awsClusterID, shouldOverride)
  456. if err != nil {
  457. return nil, telemetry.Error(ctx, span, err, "unable to get AWS bearer token")
  458. }
  459. // add this as a bearer token
  460. authInfoMap[authInfoName].Token = tok
  461. case models.DO:
  462. oauthInt, err := conf.Repo.OAuthIntegration().ReadOAuthIntegration(
  463. cluster.ProjectID,
  464. cluster.DOIntegrationID,
  465. )
  466. if err != nil {
  467. return nil, telemetry.Error(ctx, span, err, "error reading oauth integration")
  468. }
  469. telemetry.WithAttributes(span,
  470. telemetry.AttributeKV{Key: "integration-id", Value: cluster.DOIntegrationID},
  471. )
  472. tok, _, err := oauth.GetAccessToken(oauthInt.SharedOAuthModel, conf.DigitalOceanOAuth, oauth.MakeUpdateOAuthIntegrationTokenFunction(oauthInt, conf.Repo))
  473. if err != nil {
  474. return nil, telemetry.Error(ctx, span, err, "unable to get oauth access token for Digital Ocean")
  475. }
  476. // add this as a bearer token
  477. authInfoMap[authInfoName].Token = tok
  478. case models.Azure:
  479. azInt, err := conf.Repo.AzureIntegration().ReadAzureIntegration(
  480. cluster.ProjectID,
  481. cluster.AzureIntegrationID,
  482. )
  483. if err != nil {
  484. return nil, telemetry.Error(ctx, span, err, "error reading azure integration")
  485. }
  486. telemetry.WithAttributes(span,
  487. telemetry.AttributeKV{Key: "integration-id", Value: cluster.AzureIntegrationID},
  488. )
  489. authInfoMap[authInfoName].Token = string(azInt.AKSPassword)
  490. default:
  491. return nil, telemetry.Error(ctx, span, nil, "auth mechanism not supported")
  492. }
  493. // create a context of the cluster name
  494. contextMap := make(map[string]*api.Context)
  495. contextMap[cluster.Name] = &api.Context{
  496. LocationOfOrigin: cluster.ClusterLocationOfOrigin,
  497. Cluster: cluster.Name,
  498. AuthInfo: authInfoName,
  499. }
  500. apiConfig.Clusters = clusterMap
  501. apiConfig.AuthInfos = authInfoMap
  502. apiConfig.Contexts = contextMap
  503. apiConfig.CurrentContext = cluster.Name
  504. return apiConfig, nil
  505. }
  506. func (conf *OutOfClusterConfig) getTokenCache(ctx context.Context) (tok *ints.TokenCache, err error) {
  507. return &conf.Cluster.TokenCache.TokenCache, nil
  508. }
  509. func (conf *OutOfClusterConfig) setTokenCache(ctx context.Context, token string, expiry time.Time) error {
  510. _, err := conf.Repo.Cluster().UpdateClusterTokenCache(
  511. &ints.ClusterTokenCache{
  512. ClusterID: conf.Cluster.ID,
  513. TokenCache: ints.TokenCache{
  514. Token: []byte(token),
  515. Expiry: expiry,
  516. },
  517. },
  518. )
  519. return err
  520. }
  521. // NewRESTClientGetterFromInClusterConfig returns a RESTClientGetter using
  522. // default values set from the *rest.Config
  523. func NewRESTClientGetterFromInClusterConfig(conf *rest.Config, namespace string) genericclioptions.RESTClientGetter {
  524. cfs := genericclioptions.NewConfigFlags(false)
  525. if namespace != "" {
  526. cfs.Namespace = &namespace
  527. }
  528. cfs.ClusterName = &conf.ServerName
  529. cfs.Insecure = &conf.Insecure
  530. cfs.APIServer = &conf.Host
  531. cfs.CAFile = &conf.CAFile
  532. cfs.KeyFile = &conf.KeyFile
  533. cfs.CertFile = &conf.CertFile
  534. cfs.BearerToken = &conf.BearerToken
  535. cfs.Timeout = stringptr(conf.Timeout.String())
  536. cfs.Impersonate = &conf.Impersonate.UserName
  537. cfs.ImpersonateGroup = &conf.Impersonate.Groups
  538. cfs.Username = &conf.Username
  539. cfs.Password = &conf.Password
  540. return cfs
  541. }
  542. func stringptr(val string) *string {
  543. return &val
  544. }
  545. type fakeRESTClientGetter struct{}
  546. func (f *fakeRESTClientGetter) ToRESTConfig() (*rest.Config, error) {
  547. return nil, nil
  548. }
  549. func (f *fakeRESTClientGetter) ToRawKubeConfigLoader() clientcmd.ClientConfig {
  550. return nil
  551. }
  552. func (f *fakeRESTClientGetter) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
  553. return nil, nil
  554. }
  555. func (f *fakeRESTClientGetter) ToRESTMapper() (meta.RESTMapper, error) {
  556. return nil, nil
  557. }