Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audit Logs (HTTP Webhooks) #753

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions api/tasks/audit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package tasks

import (
"bytes"
"encoding/json"
"github.com/ansible-semaphore/semaphore/util"
"net/http"
"strconv"
"time"
)

func (t *task) sendAuditLog() {
if !util.Config.AuditLog {
return
}
url := util.Config.AuditLogURL
method := "POST"
payload, err := json.Marshal(map[string]interface{}{
"project_id": t.projectID,
"template_id": t.task.TemplateID,
"task_id": t.task.ID,
"playbook": t.task.Playbook,
"environment": t.task.Environment,
"start": t.task.Start,
"end": t.task.End,
"status": t.task.Status,
"task_url": util.Config.WebHost + "/project/" + strconv.Itoa(t.template.ProjectID) + "/history/?t=" + strconv.Itoa(t.task.ID),
})
requestBody := bytes.NewBuffer(payload)
if err != nil {
util.LogError(err)
return
}
client := &http.Client{Timeout: 5 * time.Second}
req, err := http.NewRequest(method, url, requestBody)

if err != nil {
util.LogError(err)
return
}
req.Header.Add("Content-Type", "application/json")

_, err = client.Do(req)
if err != nil {
util.LogError(err)
return
}
}
2 changes: 1 addition & 1 deletion api/tasks/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (t *task) updateStatus() {

sockets.Message(user, b)
}

t.sendAuditLog()
if err := t.store.UpdateTask(t.task); err != nil {
t.panicOnError(err, "Failed to update task status")
}
Expand Down
6 changes: 6 additions & 0 deletions cli/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ func InteractiveSetup(conf *util.ConfigType) {
askValue("LDAP mapping for full name field", "cn", &conf.LdapMappings.CN)
askValue("LDAP mapping for email field", "mail", &conf.LdapMappings.Mail)
}

askConfirmation("Enable audit logs?", false, &conf.AuditLog)
if conf.AuditLog {
askValue("URL to send POST audit logs to(it should start with http:// or https://)", "http://127.0.0.1:6666/", &conf.AuditLogURL)
}

}

func scanBoltDb(conf *util.ConfigType) {
Expand Down
4 changes: 4 additions & 0 deletions util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ type ConfigType struct {
EmailUsername string `json:"email_username"`
EmailPassword string `json:"email_password"`

// Audit Log
AuditLogURL string `json:"audit_log_url"`

// web host
WebHost string `json:"web_host"`

Expand Down Expand Up @@ -113,6 +116,7 @@ type ConfigType struct {
TelegramAlert bool `json:"telegram_alert"`
LdapEnable bool `json:"ldap_enable"`
LdapNeedTLS bool `json:"ldap_needtls"`
AuditLog bool `json:"audit_log"`

SshConfigPath string `json:"ssh_config_path"`

Expand Down