Semaphore/api/integration.go

231 lines
5.8 KiB
Go
Raw Normal View History

2023-07-03 01:41:13 +02:00
package api
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
2024-01-15 20:35:47 +01:00
"encoding/json"
"fmt"
2024-02-12 11:20:50 +01:00
"github.com/gorilla/context"
2024-01-15 20:35:47 +01:00
"io"
"net/http"
2023-07-03 01:41:13 +02:00
"strings"
2024-01-15 20:35:47 +01:00
2023-07-03 01:41:13 +02:00
"github.com/ansible-semaphore/semaphore/api/helpers"
"github.com/ansible-semaphore/semaphore/db"
2024-03-03 11:57:39 +01:00
log "github.com/sirupsen/logrus"
2023-07-03 01:41:13 +02:00
jsonq "github.com/thedevsaddam/gojsonq/v2"
)
// IsValidPayload checks if the github payload's hash fits with
// the hash computed by GitHub sent as a header
func IsValidPayload(secret, headerHash string, payload []byte) bool {
hash := HashPayload(secret, payload)
return hmac.Equal(
[]byte(hash),
[]byte(headerHash),
)
}
// HashPayload computes the hash of payload's body according to the webhook's secret token
// see https://developer.github.com/webhooks/securing/#validating-payloads-from-github
// returning the hash as a hexadecimal string
2024-03-22 16:39:33 +01:00
func HashPayload(secret string, payloadBody []byte) string {
hm := hmac.New(sha1.New, []byte(secret))
2024-03-22 16:39:33 +01:00
hm.Write(payloadBody)
sum := hm.Sum(nil)
return fmt.Sprintf("%x", sum)
}
2024-02-11 20:52:14 +01:00
func ReceiveIntegration(w http.ResponseWriter, r *http.Request) {
2024-03-06 22:47:01 +01:00
var err error
integrationAlias, err := helpers.GetStrParam("integration_alias", w, r)
if err != nil {
log.Error(err)
2024-03-06 22:47:01 +01:00
return
}
2024-02-11 20:52:14 +01:00
log.Info(fmt.Sprintf("Receiving Integration from: %s", r.RemoteAddr))
2023-07-03 01:41:13 +02:00
integrations, err := helpers.Store(r).GetIntegrationsByAlias(integrationAlias)
2024-02-12 11:20:50 +01:00
if err != nil {
log.Error(err)
return
}
2024-02-12 11:20:50 +01:00
for _, integration := range integrations {
switch integration.AuthMethod {
case db.IntegrationAuthHmac:
var payload []byte
_, err = r.Body.Read(payload)
if err != nil {
log.Error(err)
continue
}
if IsValidPayload(integration.AuthSecret.LoginPassword.Password, r.Header.Get(integration.AuthHeader), payload) {
log.Error(err)
continue
}
case db.IntegrationAuthToken:
if integration.AuthSecret.LoginPassword.Password != r.Header.Get(integration.AuthHeader) {
log.Error("Invalid verification token")
continue
}
case db.IntegrationAuthNone:
default:
log.Error("Unknown verification method: " + integration.AuthMethod)
continue
2024-02-12 11:20:50 +01:00
}
var matchers []db.IntegrationMatcher
matchers, err = helpers.Store(r).GetIntegrationMatchers(integration.ProjectID, db.RetrieveQueryParams{}, integration.ID)
if err != nil {
log.Error(err)
}
var matched = false
for _, matcher := range matchers {
if Match(matcher, r) {
matched = true
continue
} else {
matched = false
break
}
}
2023-07-03 01:41:13 +02:00
if !matched {
2024-03-06 14:35:16 +01:00
continue
2023-07-03 01:41:13 +02:00
}
RunIntegration(integration, r)
2023-07-03 01:41:13 +02:00
}
w.WriteHeader(http.StatusNoContent)
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func Match(matcher db.IntegrationMatcher, r *http.Request) (matched bool) {
2024-01-15 20:35:47 +01:00
switch matcher.MatchType {
2024-02-11 20:52:14 +01:00
case db.IntegrationMatchHeader:
2024-01-15 20:35:47 +01:00
var header_value = r.Header.Get(matcher.Key)
2023-07-03 01:41:13 +02:00
return MatchCompare(header_value, matcher.Method, matcher.Value)
2024-02-11 20:52:14 +01:00
case db.IntegrationMatchBody:
2023-07-03 01:41:13 +02:00
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
log.Fatalln(err)
return false
}
var body = string(bodyBytes)
2024-01-15 20:35:47 +01:00
switch matcher.BodyDataType {
2024-02-11 20:52:14 +01:00
case db.IntegrationBodyDataJSON:
2023-07-03 01:41:13 +02:00
var jsonBytes bytes.Buffer
jsonq.New().FromString(body).From(matcher.Key).Writer(&jsonBytes)
var jsonString = jsonBytes.String()
if err != nil {
log.Error(fmt.Sprintf("Failed to marshal JSON contents of body. %v", err))
}
return MatchCompare(jsonString, matcher.Method, matcher.Value)
2024-02-11 20:52:14 +01:00
case db.IntegrationBodyDataString:
2023-07-03 01:41:13 +02:00
return MatchCompare(body, matcher.Method, matcher.Value)
2024-02-11 20:52:14 +01:00
case db.IntegrationBodyDataXML:
2023-07-03 01:41:13 +02:00
// XXX: TBI
return false
}
}
2024-01-15 20:35:47 +01:00
2023-07-03 01:41:13 +02:00
return false
}
2024-02-11 20:52:14 +01:00
func MatchCompare(value string, method db.IntegrationMatchMethodType, expected string) bool {
2024-01-15 20:35:47 +01:00
switch method {
2024-02-11 20:52:14 +01:00
case db.IntegrationMatchMethodEquals:
2023-07-03 01:41:13 +02:00
return value == expected
2024-02-11 20:52:14 +01:00
case db.IntegrationMatchMethodUnEquals:
2023-07-03 01:41:13 +02:00
return value != expected
2024-02-11 20:52:14 +01:00
case db.IntegrationMatchMethodContains:
2023-07-03 01:41:13 +02:00
return strings.Contains(value, expected)
2024-01-15 20:35:47 +01:00
default:
return false
2023-07-03 01:41:13 +02:00
}
}
2024-02-11 20:52:14 +01:00
func RunIntegration(integration db.Integration, r *http.Request) {
2024-03-04 18:53:20 +01:00
project := context.Get(r, "project").(db.Project)
2023-07-03 01:41:13 +02:00
2024-02-11 20:52:14 +01:00
var extractValues = make([]db.IntegrationExtractValue, 0)
2024-03-06 14:35:16 +01:00
extractValuesForExtractor, err2 := helpers.Store(r).GetIntegrationExtractValues(project.ID, db.RetrieveQueryParams{}, integration.ID)
if err2 != nil {
log.Error(err2)
return
2023-07-03 01:41:13 +02:00
}
2024-03-06 14:35:16 +01:00
extractValues = append(extractValues, extractValuesForExtractor...)
2023-07-03 01:41:13 +02:00
var extractedResults = Extract(extractValues, r)
// XXX: LOG AN EVENT HERE
environmentJSONBytes, err := json.Marshal(extractedResults)
2024-01-15 20:35:47 +01:00
if err != nil {
log.Error(err)
return
}
2023-07-03 01:41:13 +02:00
var environmentJSONString = string(environmentJSONBytes)
var taskDefinition = db.Task{
2024-02-11 20:52:14 +01:00
TemplateID: integration.TemplateID,
ProjectID: integration.ProjectID,
2024-01-15 20:35:47 +01:00
Debug: true,
2023-07-03 01:41:13 +02:00
Environment: environmentJSONString,
}
var user db.User
user, err = helpers.Store(r).GetUser(1)
2024-01-15 20:35:47 +01:00
if err != nil {
log.Error(err)
return
}
2023-07-03 01:41:13 +02:00
_, err = helpers.TaskPool(r).AddTask(taskDefinition, &user.ID, integration.ProjectID)
if err != nil {
log.Error(err)
return
}
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func Extract(extractValues []db.IntegrationExtractValue, r *http.Request) (result map[string]string) {
2023-07-03 01:41:13 +02:00
result = make(map[string]string)
for _, extractValue := range extractValues {
2024-01-15 20:35:47 +01:00
switch extractValue.ValueSource {
2024-02-11 20:52:14 +01:00
case db.IntegrationExtractHeaderValue:
2023-07-03 01:41:13 +02:00
result[extractValue.Variable] = r.Header.Get(extractValue.Key)
2024-02-11 20:52:14 +01:00
case db.IntegrationExtractBodyValue:
2023-07-03 01:41:13 +02:00
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
2024-01-15 20:35:47 +01:00
log.Fatal(err)
2023-07-03 01:41:13 +02:00
return
}
var body = string(bodyBytes)
2024-01-15 20:35:47 +01:00
switch extractValue.BodyDataType {
2024-02-11 20:52:14 +01:00
case db.IntegrationBodyDataJSON:
2023-07-03 01:41:13 +02:00
var jsonBytes bytes.Buffer
jsonq.New().FromString(body).From(extractValue.Key).Writer(&jsonBytes)
result[extractValue.Variable] = jsonBytes.String()
2024-02-11 20:52:14 +01:00
case db.IntegrationBodyDataString:
2023-07-03 01:41:13 +02:00
result[extractValue.Variable] = body
2024-02-11 20:52:14 +01:00
case db.IntegrationBodyDataXML:
2023-07-03 01:41:13 +02:00
// XXX: TBI
}
}
}
return
}