resolver.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. package resolver
  2. import (
  3. "encoding/base64"
  4. "errors"
  5. "net/url"
  6. "strings"
  7. "github.com/porter-dev/porter/api/types"
  8. "github.com/porter-dev/porter/internal/features"
  9. "github.com/porter-dev/porter/internal/kubernetes"
  10. "github.com/porter-dev/porter/internal/models"
  11. "github.com/porter-dev/porter/internal/repository"
  12. "k8s.io/client-go/tools/clientcmd/api"
  13. ints "github.com/porter-dev/porter/internal/models/integrations"
  14. )
  15. // CandidateResolver will resolve a cluster candidate to create a new cluster
  16. type CandidateResolver struct {
  17. Resolver *types.ClusterResolverAll
  18. ClusterCandidateID uint
  19. ProjectID uint
  20. UserID uint
  21. // populated during the ResolveIntegration step
  22. integrationID uint
  23. clusterCandidate *models.ClusterCandidate
  24. rawConf *api.Config
  25. }
  26. // ResolveIntegration creates an integration in the DB
  27. func (rcf *CandidateResolver) ResolveIntegration(
  28. repo repository.Repository,
  29. ) error {
  30. cc, err := repo.Cluster().ReadClusterCandidate(rcf.ProjectID, rcf.ClusterCandidateID)
  31. if err != nil {
  32. return err
  33. }
  34. rcf.clusterCandidate = cc
  35. rawConf, err := kubernetes.GetRawConfigFromBytes(cc.Kubeconfig)
  36. if err != nil {
  37. return err
  38. }
  39. rcf.rawConf = rawConf
  40. context := rawConf.Contexts[rawConf.CurrentContext]
  41. authInfoName := context.AuthInfo
  42. authInfo := rawConf.AuthInfos[authInfoName]
  43. // iterate through the resolvers, and use the ClusterResolverAll to populate
  44. // the required fields
  45. var id uint
  46. switch cc.AuthMechanism {
  47. case models.X509:
  48. id, err = rcf.resolveX509(repo, authInfo)
  49. case models.Bearer:
  50. id, err = rcf.resolveToken(repo, authInfo)
  51. case models.Basic:
  52. id, err = rcf.resolveBasic(repo, authInfo)
  53. case models.Local:
  54. id, err = rcf.resolveLocal(repo, authInfo)
  55. case models.OIDC:
  56. id, err = rcf.resolveOIDC(repo, authInfo)
  57. case models.GCP:
  58. id, err = rcf.resolveGCP(repo, authInfo)
  59. case models.AWS:
  60. id, err = rcf.resolveAWS(repo, authInfo)
  61. }
  62. if err != nil {
  63. return err
  64. }
  65. rcf.integrationID = id
  66. return nil
  67. }
  68. func (rcf *CandidateResolver) resolveX509(
  69. repo repository.Repository,
  70. authInfo *api.AuthInfo,
  71. ) (uint, error) {
  72. ki := &ints.KubeIntegration{
  73. Mechanism: ints.KubeX509,
  74. UserID: rcf.UserID,
  75. ProjectID: rcf.ProjectID,
  76. }
  77. // attempt to construct cert and key from raw config
  78. if len(authInfo.ClientCertificateData) > 0 {
  79. ki.ClientCertificateData = authInfo.ClientCertificateData
  80. }
  81. if len(authInfo.ClientKeyData) > 0 {
  82. ki.ClientKeyData = authInfo.ClientKeyData
  83. }
  84. // override with resolver
  85. if rcf.Resolver.ClientCertData != "" {
  86. decoded, err := base64.StdEncoding.DecodeString(rcf.Resolver.ClientCertData)
  87. if err != nil {
  88. return 0, err
  89. }
  90. ki.ClientCertificateData = decoded
  91. }
  92. if rcf.Resolver.ClientKeyData != "" {
  93. decoded, err := base64.StdEncoding.DecodeString(rcf.Resolver.ClientKeyData)
  94. if err != nil {
  95. return 0, err
  96. }
  97. ki.ClientKeyData = decoded
  98. }
  99. // if resolvable, write kube integration to repo
  100. if len(ki.ClientCertificateData) == 0 || len(ki.ClientKeyData) == 0 {
  101. return 0, errors.New("could not resolve kube integration (x509)")
  102. }
  103. // return integration id if exists
  104. ki, err := repo.KubeIntegration().CreateKubeIntegration(ki)
  105. if err != nil {
  106. return 0, err
  107. }
  108. return ki.Model.ID, nil
  109. }
  110. func (rcf *CandidateResolver) resolveToken(
  111. repo repository.Repository,
  112. authInfo *api.AuthInfo,
  113. ) (uint, error) {
  114. ki := &ints.KubeIntegration{
  115. Mechanism: ints.KubeBearer,
  116. UserID: rcf.UserID,
  117. ProjectID: rcf.ProjectID,
  118. }
  119. // attempt to construct token from raw config
  120. if len(authInfo.Token) > 0 {
  121. ki.Token = []byte(authInfo.Token)
  122. }
  123. // supplement with resolver
  124. if rcf.Resolver.TokenData != "" {
  125. ki.Token = []byte(rcf.Resolver.TokenData)
  126. }
  127. // if resolvable, write kube integration to repo
  128. if len(ki.Token) == 0 {
  129. return 0, errors.New("could not resolve kube integration (token)")
  130. }
  131. // return integration id if exists
  132. ki, err := repo.KubeIntegration().CreateKubeIntegration(ki)
  133. if err != nil {
  134. return 0, err
  135. }
  136. return ki.Model.ID, nil
  137. }
  138. func (rcf *CandidateResolver) resolveBasic(
  139. repo repository.Repository,
  140. authInfo *api.AuthInfo,
  141. ) (uint, error) {
  142. ki := &ints.KubeIntegration{
  143. Mechanism: ints.KubeBasic,
  144. UserID: rcf.UserID,
  145. ProjectID: rcf.ProjectID,
  146. }
  147. if len(authInfo.Username) > 0 {
  148. ki.Username = []byte(authInfo.Username)
  149. }
  150. if len(authInfo.Password) > 0 {
  151. ki.Password = []byte(authInfo.Password)
  152. }
  153. if len(ki.Username) == 0 || len(ki.Password) == 0 {
  154. return 0, errors.New("could not resolve kube integration (basic)")
  155. }
  156. // return integration id if exists
  157. ki, err := repo.KubeIntegration().CreateKubeIntegration(ki)
  158. if err != nil {
  159. return 0, err
  160. }
  161. return ki.Model.ID, nil
  162. }
  163. func (rcf *CandidateResolver) resolveLocal(
  164. repo repository.Repository,
  165. authInfo *api.AuthInfo,
  166. ) (uint, error) {
  167. ki := &ints.KubeIntegration{
  168. Mechanism: ints.KubeLocal,
  169. UserID: rcf.UserID,
  170. ProjectID: rcf.ProjectID,
  171. Kubeconfig: rcf.clusterCandidate.Kubeconfig,
  172. }
  173. // return integration id if exists
  174. ki, err := repo.KubeIntegration().CreateKubeIntegration(ki)
  175. if err != nil {
  176. return 0, err
  177. }
  178. return ki.Model.ID, nil
  179. }
  180. func (rcf *CandidateResolver) resolveOIDC(
  181. repo repository.Repository,
  182. authInfo *api.AuthInfo,
  183. ) (uint, error) {
  184. oidc := &ints.OIDCIntegration{
  185. Client: ints.OIDCKube,
  186. UserID: rcf.UserID,
  187. ProjectID: rcf.ProjectID,
  188. }
  189. if url, ok := authInfo.AuthProvider.Config["idp-issuer-url"]; ok {
  190. oidc.IssuerURL = []byte(url)
  191. }
  192. if clientID, ok := authInfo.AuthProvider.Config["client-id"]; ok {
  193. oidc.ClientID = []byte(clientID)
  194. }
  195. if clientSecret, ok := authInfo.AuthProvider.Config["client-secret"]; ok {
  196. oidc.ClientSecret = []byte(clientSecret)
  197. }
  198. if caData, ok := authInfo.AuthProvider.Config["idp-certificate-authority-data"]; ok {
  199. // based on the implementation, the oidc plugin expects the data to be base64 encoded,
  200. // which means we will not decode it here
  201. // reference: https://github.com/kubernetes/kubernetes/blob/9dfb4c876bfca7a5ae84259fae2bc337ed90c2d7/staging/src/k8s.io/client-go/plugin/pkg/client/auth/oidc/oidc.go#L135
  202. oidc.CertificateAuthorityData = []byte(caData)
  203. }
  204. if idToken, ok := authInfo.AuthProvider.Config["id-token"]; ok {
  205. oidc.IDToken = []byte(idToken)
  206. }
  207. if refreshToken, ok := authInfo.AuthProvider.Config["refresh-token"]; ok {
  208. oidc.RefreshToken = []byte(refreshToken)
  209. }
  210. // override with resolver
  211. if rcf.Resolver.OIDCIssuerCAData != "" {
  212. // based on the implementation, the oidc plugin expects the data to be base64 encoded,
  213. // which means we will not decode it here
  214. // reference: https://github.com/kubernetes/kubernetes/blob/9dfb4c876bfca7a5ae84259fae2bc337ed90c2d7/staging/src/k8s.io/client-go/plugin/pkg/client/auth/oidc/oidc.go#L135
  215. oidc.CertificateAuthorityData = []byte(rcf.Resolver.OIDCIssuerCAData)
  216. }
  217. // return integration id if exists
  218. oidc, err := repo.OIDCIntegration().CreateOIDCIntegration(oidc)
  219. if err != nil {
  220. return 0, err
  221. }
  222. return oidc.Model.ID, nil
  223. }
  224. func (rcf *CandidateResolver) resolveGCP(
  225. repo repository.Repository,
  226. authInfo *api.AuthInfo,
  227. ) (uint, error) {
  228. // TODO -- add GCP project ID and GCP email so that source is trackable
  229. gcp := &ints.GCPIntegration{
  230. UserID: rcf.UserID,
  231. ProjectID: rcf.ProjectID,
  232. }
  233. // supplement with resolver
  234. if rcf.Resolver.GCPKeyData != "" {
  235. gcp.GCPKeyData = []byte(rcf.Resolver.GCPKeyData)
  236. }
  237. // throw error if no data
  238. if len(gcp.GCPKeyData) == 0 {
  239. return 0, errors.New("could not resolve gcp integration")
  240. }
  241. // return integration id if exists
  242. gcp, err := repo.GCPIntegration().CreateGCPIntegration(gcp)
  243. if err != nil {
  244. return 0, err
  245. }
  246. return gcp.Model.ID, nil
  247. }
  248. func (rcf *CandidateResolver) resolveAWS(
  249. repo repository.Repository,
  250. authInfo *api.AuthInfo,
  251. ) (uint, error) {
  252. // TODO -- add AWS session token as an optional param
  253. // TODO -- add AWS entity and user ARN
  254. aws := &ints.AWSIntegration{
  255. UserID: rcf.UserID,
  256. ProjectID: rcf.ProjectID,
  257. }
  258. // override with resolver
  259. if rcf.Resolver.AWSClusterID != "" {
  260. aws.AWSClusterID = []byte(rcf.Resolver.AWSClusterID)
  261. }
  262. if rcf.Resolver.AWSAccessKeyID != "" {
  263. aws.AWSAccessKeyID = []byte(rcf.Resolver.AWSAccessKeyID)
  264. }
  265. if rcf.Resolver.AWSSecretAccessKey != "" {
  266. aws.AWSSecretAccessKey = []byte(rcf.Resolver.AWSSecretAccessKey)
  267. }
  268. // throw error if no data
  269. if len(aws.AWSClusterID) == 0 || len(aws.AWSAccessKeyID) == 0 || len(aws.AWSSecretAccessKey) == 0 {
  270. return 0, errors.New("could not resolve aws integration")
  271. }
  272. // return integration id if exists
  273. aws, err := repo.AWSIntegration().CreateAWSIntegration(aws)
  274. if err != nil {
  275. return 0, err
  276. }
  277. return aws.Model.ID, nil
  278. }
  279. // ResolveCluster writes a new cluster to the DB -- this must be called after
  280. // rcf.ResolveIntegration, since it relies on the previously created integration.
  281. func (rcf *CandidateResolver) ResolveCluster(
  282. repo repository.Repository,
  283. launchDarklyClient *features.Client,
  284. ) (*models.Cluster, error) {
  285. // build a cluster from the candidate
  286. cluster, err := rcf.buildCluster()
  287. if err != nil {
  288. return nil, err
  289. }
  290. // save cluster to db
  291. return repo.Cluster().CreateCluster(cluster, launchDarklyClient)
  292. }
  293. func (rcf *CandidateResolver) buildCluster() (*models.Cluster, error) {
  294. rawConf := rcf.rawConf
  295. kcContext := rawConf.Contexts[rawConf.CurrentContext]
  296. kcAuthInfoName := kcContext.AuthInfo
  297. kcAuthInfo := rawConf.AuthInfos[kcAuthInfoName]
  298. kcClusterName := kcContext.Cluster
  299. kcCluster := rawConf.Clusters[kcClusterName]
  300. cc := rcf.clusterCandidate
  301. cluster := &models.Cluster{
  302. AuthMechanism: cc.AuthMechanism,
  303. ProjectID: cc.ProjectID,
  304. Name: cc.Name,
  305. Server: cc.Server,
  306. ClusterLocationOfOrigin: kcCluster.LocationOfOrigin,
  307. TLSServerName: kcCluster.TLSServerName,
  308. InsecureSkipTLSVerify: kcCluster.InsecureSkipTLSVerify,
  309. UserLocationOfOrigin: kcAuthInfo.LocationOfOrigin,
  310. UserImpersonate: kcAuthInfo.Impersonate,
  311. }
  312. if len(kcAuthInfo.ImpersonateGroups) > 0 {
  313. cluster.UserImpersonateGroups = strings.Join(kcAuthInfo.ImpersonateGroups, ",")
  314. }
  315. if len(kcCluster.CertificateAuthorityData) > 0 {
  316. cluster.CertificateAuthorityData = kcCluster.CertificateAuthorityData
  317. }
  318. if rcf.Resolver.ClusterCAData != "" {
  319. decoded, err := base64.StdEncoding.DecodeString(rcf.Resolver.ClusterCAData)
  320. // skip if decoding error
  321. if err != nil {
  322. return nil, err
  323. }
  324. cluster.CertificateAuthorityData = decoded
  325. }
  326. if rcf.Resolver.ClusterHostname != "" {
  327. serverURL, err := url.Parse(cluster.Server)
  328. if err != nil {
  329. return nil, err
  330. }
  331. if serverURL.Port() == "" {
  332. serverURL.Host = rcf.Resolver.ClusterHostname
  333. } else {
  334. serverURL.Host = rcf.Resolver.ClusterHostname + ":" + serverURL.Port()
  335. }
  336. cluster.Server = serverURL.String()
  337. }
  338. switch cc.AuthMechanism {
  339. case models.X509, models.Bearer, models.Basic, models.Local:
  340. cluster.KubeIntegrationID = rcf.integrationID
  341. case models.OIDC:
  342. cluster.OIDCIntegrationID = rcf.integrationID
  343. case models.GCP:
  344. cluster.GCPIntegrationID = rcf.integrationID
  345. case models.AWS:
  346. cluster.AWSIntegrationID = rcf.integrationID
  347. }
  348. return cluster, nil
  349. }