Semaphore/db/Integration.go

198 lines
5.6 KiB
Go
Raw Permalink Normal View History

2023-07-03 01:41:13 +02:00
package db
import (
"strconv"
"strings"
2023-07-03 01:41:13 +02:00
)
type IntegrationAuthMethod string
const (
2024-03-22 23:01:32 +01:00
IntegrationAuthNone = ""
IntegrationAuthGitHub = "github"
IntegrationAuthToken = "token"
IntegrationAuthHmac = "hmac"
)
2024-02-11 20:52:14 +01:00
type IntegrationMatchType string
2023-07-03 01:41:13 +02:00
const (
2024-02-11 20:52:14 +01:00
IntegrationMatchHeader IntegrationMatchType = "header"
IntegrationMatchBody IntegrationMatchType = "body"
2023-07-03 01:41:13 +02:00
)
2024-02-11 20:52:14 +01:00
type IntegrationMatchMethodType string
2023-07-03 01:41:13 +02:00
const (
2024-02-11 20:52:14 +01:00
IntegrationMatchMethodEquals IntegrationMatchMethodType = "equals"
IntegrationMatchMethodUnEquals IntegrationMatchMethodType = "unequals"
IntegrationMatchMethodContains IntegrationMatchMethodType = "contains"
2023-07-03 01:41:13 +02:00
)
2024-02-11 20:52:14 +01:00
type IntegrationBodyDataType string
2023-07-03 01:41:13 +02:00
const (
2024-02-11 20:52:14 +01:00
IntegrationBodyDataJSON IntegrationBodyDataType = "json"
IntegrationBodyDataString IntegrationBodyDataType = "string"
2023-07-03 01:41:13 +02:00
)
2024-02-11 20:52:14 +01:00
type IntegrationMatcher struct {
2024-03-06 14:28:24 +01:00
ID int `db:"id" json:"id"`
Name string `db:"name" json:"name"`
IntegrationID int `db:"integration_id" json:"integration_id"`
MatchType IntegrationMatchType `db:"match_type" json:"match_type"`
Method IntegrationMatchMethodType `db:"method" json:"method"`
BodyDataType IntegrationBodyDataType `db:"body_data_type" json:"body_data_type"`
Key string `db:"key" json:"key"`
Value string `db:"value" json:"value"`
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
type IntegrationExtractValueSource string
2023-07-03 01:41:13 +02:00
const (
2024-02-11 20:52:14 +01:00
IntegrationExtractBodyValue IntegrationExtractValueSource = "body"
IntegrationExtractHeaderValue IntegrationExtractValueSource = "header"
2023-07-03 01:41:13 +02:00
)
2024-02-11 20:52:14 +01:00
type IntegrationExtractValue struct {
2024-03-06 14:28:24 +01:00
ID int `db:"id" json:"id"`
Name string `db:"name" json:"name"`
IntegrationID int `db:"integration_id" json:"integration_id"`
ValueSource IntegrationExtractValueSource `db:"value_source" json:"value_source"`
BodyDataType IntegrationBodyDataType `db:"body_data_type" json:"body_data_type"`
Key string `db:"key" json:"key"`
Variable string `db:"variable" json:"variable"`
2023-07-03 01:41:13 +02:00
}
2024-03-07 10:32:25 +01:00
type IntegrationAlias struct {
ID int `db:"id" json:"-"`
2024-03-07 10:32:25 +01:00
Alias string `db:"alias" json:"alias"`
ProjectID int `db:"project_id" json:"project_id"`
IntegrationID *int `db:"integration_id" json:"integration_id"`
}
2024-02-11 20:52:14 +01:00
type Integration struct {
ID int `db:"id" json:"id"`
Name string `db:"name" json:"name"`
ProjectID int `db:"project_id" json:"project_id"`
TemplateID int `db:"template_id" json:"template_id"`
AuthMethod IntegrationAuthMethod `db:"auth_method" json:"auth_method"`
AuthSecretID *int `db:"auth_secret_id" json:"auth_secret_id"`
2024-02-12 10:50:01 +01:00
AuthHeader string `db:"auth_header" json:"auth_header"`
AuthSecret AccessKey `db:"-" json:"-"`
Searchable bool `db:"searchable" json:"searchable"`
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (env *Integration) Validate() error {
2023-07-03 01:41:13 +02:00
if env.Name == "" {
2024-02-11 20:52:14 +01:00
return &ValidationError{"No Name set for integration"}
2023-07-03 01:41:13 +02:00
}
return nil
}
2024-02-11 20:52:14 +01:00
func (env *IntegrationMatcher) Validate() error {
2023-07-03 01:41:13 +02:00
if env.MatchType == "" {
return &ValidationError{"No Match Type set"}
} else {
if env.Key == "" {
return &ValidationError{"No key set"}
}
if env.Value == "" {
return &ValidationError{"No value set"}
}
}
if env.Name == "" {
2024-02-11 20:52:14 +01:00
return &ValidationError{"No Name set for integration"}
2023-07-03 01:41:13 +02:00
}
return nil
}
2024-02-11 20:52:14 +01:00
func (env *IntegrationExtractValue) Validate() error {
2023-07-03 01:41:13 +02:00
if env.ValueSource == "" {
return &ValidationError{"No Value Source defined"}
}
if env.Name == "" {
2024-02-11 20:52:14 +01:00
return &ValidationError{"No Name set for integration"}
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
if env.ValueSource == IntegrationExtractBodyValue {
2023-07-03 01:41:13 +02:00
if env.BodyDataType == "" {
return &ValidationError{"Value Source but no body data type set"}
}
2024-02-11 20:52:14 +01:00
if env.BodyDataType == IntegrationBodyDataJSON {
2023-07-03 01:41:13 +02:00
if env.Key == "" {
return &ValidationError{"No Key set for JSON Body Data extraction."}
}
}
}
2024-02-11 20:52:14 +01:00
if env.ValueSource == IntegrationExtractHeaderValue {
2023-07-03 01:41:13 +02:00
if env.Key == "" {
return &ValidationError{"Value Source set but no Key set"}
}
}
return nil
}
2024-02-11 20:52:14 +01:00
func (matcher *IntegrationMatcher) String() string {
2023-07-03 01:41:13 +02:00
var builder strings.Builder
// ID:1234 body/json key == value on Extractor: 1234
builder.WriteString("ID:" + strconv.Itoa(matcher.ID) + " " + string(matcher.MatchType))
2024-02-11 20:52:14 +01:00
if matcher.MatchType == IntegrationMatchBody {
2023-07-03 01:41:13 +02:00
builder.WriteString("/" + string(matcher.BodyDataType))
}
builder.WriteString(" " + matcher.Key + " ")
switch matcher.Method {
2024-02-11 20:52:14 +01:00
case IntegrationMatchMethodEquals:
builder.WriteString("==")
2024-02-11 20:52:14 +01:00
case IntegrationMatchMethodUnEquals:
builder.WriteString("!=")
2024-02-11 20:52:14 +01:00
case IntegrationMatchMethodContains:
2023-07-03 01:41:13 +02:00
builder.WriteString(" contains ")
default:
2023-07-03 01:41:13 +02:00
}
2024-03-06 14:28:24 +01:00
builder.WriteString(matcher.Value + ", on Extractor: " + strconv.Itoa(matcher.IntegrationID))
2023-07-03 01:41:13 +02:00
return builder.String()
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (value *IntegrationExtractValue) String() string {
2023-07-03 01:41:13 +02:00
var builder strings.Builder
// ID:1234 body/json from key as argument
builder.WriteString("ID:" + strconv.Itoa(value.ID) + " " + string(value.ValueSource))
2024-02-11 20:52:14 +01:00
if value.ValueSource == IntegrationExtractBodyValue {
2023-07-03 01:41:13 +02:00
builder.WriteString("/" + string(value.BodyDataType))
}
builder.WriteString(" from " + value.Key + " as " + value.Variable)
return builder.String()
}
2024-03-22 21:44:15 +01:00
func FillIntegration(d Store, inventory *Integration) (err error) {
if inventory.AuthSecretID != nil {
inventory.AuthSecret, err = d.GetAccessKey(inventory.ProjectID, *inventory.AuthSecretID)
}
if err != nil {
return
}
err = inventory.AuthSecret.DeserializeSecret()
return
}