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

populate gcp sa email when creating gcp int

Alexander Belanger 4 лет назад
Родитель
Сommit
8d1beffc38

+ 5 - 1
api/server/handlers/project_integration/create_gcp.go

@@ -53,11 +53,15 @@ func (p *CreateGCPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 }
 
 func CreateGCPIntegration(request *types.CreateGCPRequest, projectID, userID uint) *ints.GCPIntegration {
-	return &ints.GCPIntegration{
+	resp := &ints.GCPIntegration{
 		UserID:       userID,
 		ProjectID:    projectID,
 		GCPKeyData:   []byte(request.GCPKeyData),
 		GCPProjectID: request.GCPProjectID,
 		GCPRegion:    request.GCPRegion,
 	}
+
+	resp.PopulateGCPMetadata()
+
+	return resp
 }

+ 4 - 4
api/types/project_integration.go

@@ -99,11 +99,11 @@ type GCPIntegration struct {
 	// The project that this integration belongs to
 	ProjectID uint `json:"project_id"`
 
-	// The GCP project id where the service account for this auth mechanism persists
-	GCPProjectID string `json:"gcp-project-id"`
+	// The GCP service account email for this credential
+	GCPSAEmail string `json:"gcp_sa_email"`
 
-	// The GCP user email that linked this service account
-	GCPUserEmail string `json:"gcp-user-email"`
+	// The GCP project id where the service account for this auth mechanism persists
+	GCPProjectID string `json:"gcp_project_id"`
 }
 
 type ListGCPResponse []*GCPIntegration

+ 17 - 2
internal/models/integrations/gcp.go

@@ -25,6 +25,9 @@ type GCPIntegration struct {
 	// The GCP project id where the service account for this auth mechanism persists
 	GCPProjectID string `json:"gcp_project_id"`
 
+	// The GCP service account email for this credential
+	GCPSAEmail string `json:"gcp_sa_email"`
+
 	// The GCP user email that linked this service account
 	GCPUserEmail string `json:"gcp-user-email"`
 
@@ -41,12 +44,11 @@ type GCPIntegration struct {
 
 func (g *GCPIntegration) ToGCPIntegrationType() *types.GCPIntegration {
 	return &types.GCPIntegration{
-		CreatedAt:    g.CreatedAt,
 		ID:           g.ID,
 		UserID:       g.UserID,
 		ProjectID:    g.ProjectID,
 		GCPProjectID: g.GCPProjectID,
-		GCPUserEmail: g.GCPUserEmail,
+		GCPSAEmail:   g.GCPSAEmail,
 	}
 }
 
@@ -126,3 +128,16 @@ func GCPProjectIDFromJSON(jsonData []byte) (string, error) {
 
 	return f.ProjectID, nil
 }
+
+// PopulateGCPMetadata uses the credentials file to get the GCP SA name and
+// project ID, and attaches it to the GCP credential
+func (g *GCPIntegration) PopulateGCPMetadata() {
+	var f credentialsFile
+
+	if err := json.Unmarshal(g.GCPKeyData, &f); err != nil {
+		return
+	}
+
+	g.GCPProjectID = f.ProjectID
+	g.GCPSAEmail = f.ClientEmail
+}