Skip to content

Commit

Permalink
feat: update specs
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamadch91 committed Dec 21, 2024
1 parent 3a1d9b2 commit e74e273
Show file tree
Hide file tree
Showing 18 changed files with 433 additions and 155 deletions.
18 changes: 17 additions & 1 deletion services/integration/api/credentials/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,23 @@ func (h API) UpdateCredential(c echo.Context) error {
h.logger.Error("failed to encrypt secret", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to encrypt config")
}
err = h.database.UpdateCredential(credentialId, secret)
masked := make(map[string]any)
for key, value := range req.Credentials {
strValue, ok := value.(string) // Ensure the value is a string
if !ok {
// If it's not a string, just skip masking
masked[key] = "not available"
continue
}

// Get the last 5 characters, or the full string if it's shorter
if len(strValue) > 5 {
masked[key] = "*****" + strValue[len(strValue)-5:]
} else {
masked[key] = "*****" + strValue
}
}
err = h.database.UpdateCredential(credentialId, secret,masked,req.Description)
if err != nil {
h.logger.Error("failed to update credential", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to update credential")
Expand Down
53 changes: 52 additions & 1 deletion services/integration/api/integrations/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,45 @@ func (h API) DiscoverIntegrations(c echo.Context) error {
h.logger.Error("failed to encrypt secret", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to encrypt config")
}
masked := make(map[string]any)
for key, value := range req.Credentials {
strValue, ok := value.(string) // Ensure the value is a string
if !ok {
// If it's not a string, just skip masking
masked[key] = "not available"
continue
}

// Get the last 5 characters, or the full string if it's shorter
if len(strValue) > 5 {
masked[key] = "*****" + strValue[len(strValue)-5:]
} else {
masked[key] = "*****" + strValue
}
}
// convert to jsonb
maskedSecreyJsonData, err := json.Marshal(masked)
maskedSecretJsonb := pgtype.JSONB{}
err = maskedSecretJsonb.Set(maskedSecreyJsonData)
if err != nil {
h.logger.Error("failed to set masked secret", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to set masked secret")
}


credentialID := uuid.New()

metadata := make(map[string]string)
metadataJsonData, err := json.Marshal(metadata)
credentialMetadataJsonb := pgtype.JSONB{}

err = credentialMetadataJsonb.Set(metadataJsonData)
err = h.database.CreateCredential(&models2.Credential{
ID: credentialID,
IntegrationType: req.IntegrationType,
CredentialType: req.CredentialType,
Description: req.Description,
MaskedSecret: maskedSecretJsonb,
Secret: secret,
Metadata: credentialMetadataJsonb,
})
Expand Down Expand Up @@ -338,6 +366,8 @@ func (h API) AddIntegrations(c echo.Context) error {
for _, i := range integrationTypeIntegrations {
integrationTypeIntegrationsMap[i.ProviderID] = true
}
//
var count = 0

for _, i := range integrations {
if _, ok := providerIDs[i.ProviderID]; !ok {
Expand Down Expand Up @@ -387,7 +417,11 @@ func (h API) AddIntegrations(c echo.Context) error {
h.logger.Error("failed to create integration", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to create integration")
}
count++
// update credentials
}
err= h.database.UpdateCredentialIntegrationCount(req.CredentialID,count)


return c.NoContent(http.StatusOK)
}
Expand Down Expand Up @@ -789,12 +823,29 @@ func (h API) Update(c echo.Context) error {
h.logger.Error("failed to encrypt secret", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to encrypt config")
}
masked := make(map[string]any)
for key, value := range req.Credentials {
strValue, ok := value.(string) // Ensure the value is a string
if !ok {
// If it's not a string, just skip masking
masked[key] = "not available"
continue
}

// Get the last 5 characters, or the full string if it's shorter
if len(strValue) > 5 {
masked[key] = "*****" + strValue[len(strValue)-5:]
} else {
masked[key] = "*****" + strValue
}
}

err = h.database.UpdateCredential(integration.CredentialID.String(), secret)
err = h.database.UpdateCredential(integration.CredentialID.String(), secret,masked,req.Description)
if err != nil {
h.logger.Error("failed to update credential", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to update credential")
}


return c.NoContent(http.StatusOK)
}
Expand Down
4 changes: 4 additions & 0 deletions services/integration/api/models/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type Credential struct {
IntegrationType integration.Type `json:"integration_type"`
CredentialType string `json:"credential_type"`
Metadata map[string]string `json:"metadata"`
IntegrationCount int `json:"integration_count"`
MaskedSecret map[string]string `json:"masked_secret"`
Description string `json:"description"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
Expand All @@ -22,6 +25,7 @@ type ListCredentialsRequest struct {

type UpdateCredentialRequest struct {
Credentials map[string]any `json:"credentials"`
Description string `json:"description"`
}

type ListCredentialsResponse struct {
Expand Down
2 changes: 2 additions & 0 deletions services/integration/api/models/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
type DiscoverIntegrationRequest struct {
IntegrationType integration.Type `json:"integration_type"`
CredentialType string `json:"credential_type"`
Description string `json:"description"`
CredentialID *string `json:"credential_id"`
Credentials map[string]any `json:"credentials"`
}
Expand All @@ -34,6 +35,7 @@ type AddIntegrationsRequest struct {

type UpdateRequest struct {
Credentials map[string]any `json:"credentials"`
Description string `json:"description"`
}

type Integration struct {
Expand Down
27 changes: 25 additions & 2 deletions services/integration/db/credential.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package db

import (
"encoding/json"
"fmt"

"github.com/jackc/pgtype"
"github.com/opengovern/opencomply/services/integration/models"
"gorm.io/gorm/clause"
)
Expand Down Expand Up @@ -83,14 +86,34 @@ func (db Database) GetCredential(id string) (*models.Credential, error) {
return &credential, nil
}

func (db Database) UpdateCredential(id string, secret string) error {
func (db Database) UpdateCredential(id string, secret string,masked map[string]any,description string) error {
maskedSecreyJsonData, err := json.Marshal(masked)
maskedSecretJsonb := pgtype.JSONB{}
err = maskedSecretJsonb.Set(maskedSecreyJsonData)
if err != nil {
return err
}
tx := db.Orm.
Model(&models.Credential{}).
Where("id = ?", id).Update("secret", secret)
Where("id = ?", id).Update("secret", secret).Update("masked_secret", maskedSecretJsonb).Update("description", description)

if tx.Error != nil {
return tx.Error
}

return nil
}

// update descripion and integration count
func (db Database) UpdateCredentialIntegrationCount(id string, count int) error {
tx:= db.Orm.
Model(&models.Credential{}).
Where("id = ?", id).
Update("integration_count", count)

if tx.Error != nil {
return tx.Error
}
return nil
}

Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,19 @@
"sortable": true,
"filterable": true,
"info": "ID.",
"detail": true,
"detail": false,
"show": true,
"detail_order": 1
},
{
"name": "created_at",
"label": "Created At",
"fieldType": "date",
"name": "description",
"label": "Description",
"fieldType": "text",
"order": 2,
"sortable": true,
"filterable": true,
"info": "Timestamp when the integration was created.",
"show": true,
"info": "Description of the credential.",
"detail": true,
"detail_order": 2
},
Expand All @@ -250,23 +252,35 @@
"order": 3,
"sortable": true,
"filterable": true,
"info": "Timestamp when the integration was last updated.",
"detail": true,
"show": true,
"info": "Timestamp when the credential was last updated.",
"detail": false,
"detail_order": 3
},
{
"name": "integration_count",
"label": "Integration Count",
"fieldType": "text",
"order": 4,
"sortable": true,
"filterable": true,
"show": true,
"info": "Number of active integrations using this credential.",
"detail": true,
"detail_order": 4
},
{
"name": "credential_type",
"label": "Credential Type",
"fieldType": "text",
"required": true,
"order": 4,
"info": "Type of Credential used (Single Account/Multi-Account).",
"order": 5,
"show": false,
"info": "Type of Credential used (Classic PAT).",
"valueMap": {
"aws_single_account": "Single Account",
"aws_multi_account": "Multi-Account"
"classic_pat": "Classic Personal Access Token (PAT)"
},
"detail": true,
"detail_order": 3
"detail_order": 5
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,19 @@
"sortable": true,
"filterable": true,
"info": "ID.",
"detail": true,
"detail": false,
"show": true,
"detail_order": 1
},
{
"name": "created_at",
"label": "Created At",
"fieldType": "date",
"name": "description",
"label": "Description",
"fieldType": "text",
"order": 2,
"sortable": true,
"filterable": true,
"info": "Timestamp when the integration was created.",
"show": true,
"info": "Description of the credential.",
"detail": true,
"detail_order": 2
},
Expand All @@ -273,23 +275,35 @@
"order": 3,
"sortable": true,
"filterable": true,
"info": "Timestamp when the integration was last updated.",
"detail": true,
"show": true,
"info": "Timestamp when the credential was last updated.",
"detail": false,
"detail_order": 3
},
{
"name": "integration_count",
"label": "Integration Count",
"fieldType": "text",
"order": 4,
"sortable": true,
"filterable": true,
"show": true,
"info": "Number of active integrations using this credential.",
"detail": true,
"detail_order": 4
},
{
"name": "credential_type",
"label": "Credential Type",
"fieldType": "text",
"required": true,
"order": 4,
"info": "Type of Credential used (SPN Password Based/SPN Certificate).",
"order": 5,
"show": false,
"info": "Type of Credential used (Classic PAT).",
"valueMap": {
"spn_password_based": "SPN Password Based",
"spn_certificate": "SPN Certificate"
"classic_pat": "Classic Personal Access Token (PAT)"
},
"detail": false,
"detail_order": 4
"detail": true,
"detail_order": 5
}
]
},
Expand Down
Loading

0 comments on commit e74e273

Please sign in to comment.