Просмотр исходного кода

add create integration db method

Mohammed Nafees 3 лет назад
Родитель
Сommit
616501bcba

+ 44 - 0
api/server/handlers/saml/create_integration.go

@@ -2,7 +2,10 @@ package saml
 
 import (
 	"errors"
+	"fmt"
 	"net/http"
+	"net/url"
+	"strings"
 
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"
@@ -10,6 +13,8 @@ import (
 	"github.com/porter-dev/porter/api/server/shared/config"
 	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/internal/models"
+	"github.com/porter-dev/porter/internal/models/saml"
+	"gorm.io/gorm"
 )
 
 type CreateSAMLIntegrationHandler struct {
@@ -36,4 +41,43 @@ func (h *CreateSAMLIntegrationHandler) ServeHTTP(w http.ResponseWriter, r *http.
 
 	// FIXME: check if user has necessary permissions to make this request with RBAC
 
+	request := &types.CreateSAMLIntegrationRequest{}
+
+	if ok := h.DecodeAndValidate(w, r, request); !ok {
+		return
+	}
+
+	for _, domain := range request.Domains {
+		parsed, err := url.Parse("https://" + domain)
+
+		if err != nil || parsed.Host != domain {
+			h.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("invalid domain %s", domain),
+				http.StatusBadRequest))
+			return
+		}
+
+		_, err = h.Repo().SAMLIntegration().ValidateSAMLIntegration(domain)
+
+		if err == nil {
+			h.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
+				fmt.Errorf("domain %s already exists in another SAML integration, please talk to the Porter team for help",
+					domain), http.StatusBadRequest))
+			return
+		} else if !errors.Is(err, gorm.ErrRecordNotFound) {
+			h.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+			return
+		}
+	}
+
+	integ := &saml.SAMLIntegration{
+		ProjectID:       project.ID,
+		Domains:         strings.Join(request.Domains, ","),
+		SignOnURL:       request.SignOnURL,
+		CertificateData: request.CertificateData,
+	}
+
+	if _, err := h.Repo().SAMLIntegration().CreateSAMLIntegration(integ); err != nil {
+		h.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
 }

+ 8 - 1
api/types/saml.go

@@ -7,5 +7,12 @@ const (
 )
 
 type ValidateSAMLRequest struct {
-	Email string `json:"email"`
+	Email string `json:"email" form:"required,email"`
+}
+
+type CreateSAMLIntegrationRequest struct {
+	Domains         []string `json:"domains" form:"required,fqdn"`
+	Type            IDPType  `json:"type" form:"required,oneof=okta"`
+	SignOnURL       string   `json:"sign_on_url" form:"required,url"`
+	CertificateData []byte   `json:"certificate_data" form:"required"`
 }

+ 0 - 1
ee/migrate/migrate_vault.go

@@ -1,5 +1,4 @@
 //go:build ee
-// +build ee
 
 package migrate
 

+ 6 - 4
internal/models/saml/saml_integration.go

@@ -8,8 +8,10 @@ import (
 type SAMLIntegration struct {
 	gorm.Model
 
-	Domains       string
-	IntegrationID uint
-	Type          types.IDPType
-	SignOnURL     string
+	ProjectID uint
+
+	Domains         string
+	Type            types.IDPType
+	SignOnURL       string
+	CertificateData []byte
 }

+ 8 - 0
internal/repository/credentials/credentials.go

@@ -62,6 +62,11 @@ type GitlabCredential struct {
 	AppClientSecret []byte `json:"app_client_secret"`
 }
 
+type SAMLCredential struct {
+	AppClientID     []byte `json:"app_client_id"`
+	AppClientSecret []byte `json:"app_client_secret"`
+}
+
 type CredentialStorage interface {
 	// OAuth
 	WriteOAuthCredential(oauthIntegration *integrations.OAuthIntegration, data *OAuthCredential) error
@@ -87,4 +92,7 @@ type CredentialStorage interface {
 	WriteGitlabCredential(giIntegration *integrations.GitlabIntegration, data *GitlabCredential) error
 	GetGitlabCredential(giIntegration *integrations.GitlabIntegration) (*GitlabCredential, error)
 	CreateGitlabToken(giIntegration *integrations.GitlabIntegration) (string, error)
+
+	// SAML
+
 }

+ 2 - 0
internal/repository/gorm/migrate.go

@@ -3,6 +3,7 @@ package gorm
 import (
 	"github.com/porter-dev/porter/internal/models"
 	ints "github.com/porter-dev/porter/internal/models/integrations"
+	"github.com/porter-dev/porter/internal/models/saml"
 
 	"gorm.io/gorm"
 )
@@ -74,5 +75,6 @@ func AutoMigrate(db *gorm.DB, debug bool) error {
 		&ints.GithubAppInstallation{},
 		&ints.GithubAppOAuthIntegration{},
 		&ints.SlackIntegration{},
+		&saml.SAMLIntegration{},
 	)
 }

+ 1 - 1
internal/repository/gorm/repository.go

@@ -270,6 +270,6 @@ func NewRepository(db *gorm.DB, key *[32]byte, storageBackend credentials.Creden
 		tag:                       NewTagRepository(db),
 		stack:                     NewStackRepository(db),
 		monitor:                   NewMonitorTestResultRepository(db),
-		samlIntegration:           NewSAMLIntegrationRepository(db),
+		samlIntegration:           NewSAMLIntegrationRepository(db, key, storageBackend),
 	}
 }

+ 14 - 3
internal/repository/gorm/saml.go

@@ -3,18 +3,25 @@ package gorm
 import (
 	"github.com/porter-dev/porter/internal/models/saml"
 	"github.com/porter-dev/porter/internal/repository"
+	"github.com/porter-dev/porter/internal/repository/credentials"
 	"gorm.io/gorm"
 )
 
 // SAMLIntegrationRepository uses gorm.DB for querying the database
 type SAMLIntegrationRepository struct {
-	db *gorm.DB
+	db             *gorm.DB
+	key            *[32]byte
+	storageBackend credentials.CredentialStorage
 }
 
 // NewSAMLIntegrationRepository returns a SAMLIntegrationRepository which uses
 // gorm.DB for querying the database
-func NewSAMLIntegrationRepository(db *gorm.DB) repository.SAMLIntegrationRepository {
-	return &SAMLIntegrationRepository{db}
+func NewSAMLIntegrationRepository(
+	db *gorm.DB,
+	key *[32]byte,
+	storageBackend credentials.CredentialStorage,
+) repository.SAMLIntegrationRepository {
+	return &SAMLIntegrationRepository{db, key, storageBackend}
 }
 
 func (repo *SAMLIntegrationRepository) ValidateSAMLIntegration(domain string) (*saml.SAMLIntegration, error) {
@@ -28,3 +35,7 @@ func (repo *SAMLIntegrationRepository) ValidateSAMLIntegration(domain string) (*
 
 	return integ, nil
 }
+
+func (repo *SAMLIntegrationRepository) CreateSAMLIntegration(integ *saml.SAMLIntegration) (*saml.SAMLIntegration, error) {
+	return nil, nil
+}

+ 2 - 1
internal/repository/saml.go

@@ -5,5 +5,6 @@ import (
 )
 
 type SAMLIntegrationRepository interface {
-	ValidateSAMLIntegration(domain string) (*saml.SAMLIntegration, error)
+	ValidateSAMLIntegration(string) (*saml.SAMLIntegration, error)
+	CreateSAMLIntegration(*saml.SAMLIntegration) (*saml.SAMLIntegration, error)
 }

+ 6 - 0
internal/repository/test/saml.go

@@ -26,3 +26,9 @@ func (repo *SAMLIntegrationRepository) ValidateSAMLIntegration(domain string) (*
 
 	return integ, nil
 }
+
+func (repo *SAMLIntegrationRepository) CreateSAMLIntegration(integ *saml.SAMLIntegration) (*saml.SAMLIntegration, error) {
+	repo.integrations = append(repo.integrations, integ)
+
+	return integ, nil
+}