cluster.go 13 KB

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