resolver.go 11 KB

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