Skip to content

Commit

Permalink
Merge pull request #169 from Issif/kubernetes-logs
Browse files Browse the repository at this point in the history
new actionner kubernetes:log
  • Loading branch information
Issif authored Jan 26, 2024
2 parents 8601e5d + 7a58b7b commit c92d8a3
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 7 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [`kubernetes:networkpolicy`](#kubernetesnetworkpolicy)
- [`kubernetes:exec`](#kubernetesexec)
- [`kubernetes:script`](#kubernetesscript)
- [`kubernetes:log`](#kuberneteslog)
- [Notifiers](#notifiers)
- [K8s Events](#k8s-events)
- [Slack](#slack)
Expand Down Expand Up @@ -116,6 +117,16 @@ Each `actionner` is configured with:
* `k8s.pod.name`
* `k8s.ns.name`

### `kubernetes:log`

* Description: **Get logs from a pod**
* Continue: `true`
* Parameters:
* `tail_lines`: The number of lines from the end of the logs to show (default: `1000`)
* Required fields:
* `k8s.pod.name`
* `k8s.ns.name`

## Notifiers

`Notifiers` define which outputs to notify with result of actions.
Expand Down
15 changes: 14 additions & 1 deletion actionners/actionners.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/Issif/falco-talon/actionners/kubernetes/exec"
labelize "github.com/Issif/falco-talon/actionners/kubernetes/labelize"
logActionner "github.com/Issif/falco-talon/actionners/kubernetes/log"
networkpolicy "github.com/Issif/falco-talon/actionners/kubernetes/networkpolicy"
"github.com/Issif/falco-talon/actionners/kubernetes/script"
terminate "github.com/Issif/falco-talon/actionners/kubernetes/terminate"
Expand Down Expand Up @@ -97,7 +98,19 @@ func GetDefaultActionners() *Actionners {
},
CheckParameters: script.CheckParameters,
Action: script.Script,
})
},
&Actionner{
Category: "kubernetes",
Name: "log",
DefaultContinue: true,
Init: kubernetes.Init,
Checks: []checkActionner{
kubernetes.CheckPodExist,
},
CheckParameters: logActionner.CheckParameters,
Action: logActionner.Log,
},
)
}

return defaultActionners
Expand Down
109 changes: 109 additions & 0 deletions actionners/kubernetes/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package log

import (
"bytes"
"context"
"fmt"
"io"

corev1 "k8s.io/api/core/v1"

"github.com/Issif/falco-talon/internal/events"
kubernetes "github.com/Issif/falco-talon/internal/kubernetes/client"
"github.com/Issif/falco-talon/internal/rules"
"github.com/Issif/falco-talon/utils"
)

var Log = func(rule *rules.Rule, action *rules.Action, event *events.Event) (utils.LogLine, error) {
pod := event.GetPodName()
namespace := event.GetNamespaceName()

objects := map[string]string{
"Pod": pod,
"Namespace": namespace,
}

parameters := action.GetParameters()
tailLines := new(int64)
if parameters["tail_lines"] != nil {
*tailLines = int64(parameters["tail_lines"].(int))
}
if *tailLines == 0 {
*tailLines = 20
}

command := new(string)
if parameters["command"] != nil {
*command = parameters["command"].(string)
}

client := kubernetes.GetClient()

p, _ := client.GetPod(pod, namespace)
containers := kubernetes.GetContainers(p)
if len(containers) == 0 {
err := fmt.Errorf("no container found")
return utils.LogLine{
Objects: objects,
Error: err.Error(),
Status: "failure",
},
err
}

ctx := context.Background()
var output string

for i, container := range containers {
logs, err := client.Clientset.CoreV1().Pods(namespace).GetLogs(pod, &corev1.PodLogOptions{
Container: container,
TailLines: tailLines,
}).Stream(ctx)
if err != nil {
if err != nil {
if i == len(containers)-1 {
return utils.LogLine{
Objects: objects,
Error: err.Error(),
Status: "failure",
}, err
}
continue
}
}
defer logs.Close()

buf := new(bytes.Buffer)
_, err = io.Copy(buf, logs)
if err != nil {
return utils.LogLine{
Objects: objects,
Status: "failure",
Error: err.Error(),
},
err
}

output = buf.String()
if output != "" {
break
}
}

return utils.LogLine{
Objects: objects,
Output: output,
Status: "success",
},
nil
}

var CheckParameters = func(action *rules.Action) error {
parameters := action.GetParameters()
err := utils.CheckParameters(parameters, "tail_lines", utils.IntStr, nil, false)
if err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion notifiers/smtp/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var Init = func(fields map[string]interface{}) error {

var Notify = func(log utils.LogLine) error {
if smtpconfig.HostPort == "" {
return errors.New("wrong config")
return errors.New("wrong host_port")
}

payload, err := NewPayload(log)
Expand Down
10 changes: 5 additions & 5 deletions notifiers/smtp/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var htmlTmpl = `
td{font-family:arial,helvetica,sans-serif;}
</style>
<table cellpadding="3" cellspacing="0" style="font-family:arial,helvetica,sans-serif; height:2px; width:700px;">
<table cellpadding="5" cellspacing="0" style="font-family:arial,helvetica,sans-serif; height:2px; width:800px;">
<tbody>
<tr>
<td><img src="https://raw.githubusercontent.com/cncf/artwork/master/projects/falco/horizontal/color/falco-horizontal-color.png" width="117px" height="47"></td>
Expand All @@ -43,14 +43,14 @@ var htmlTmpl = `
</tbody>
</table>
<br>
<table cellpadding="3" cellspacing="0" style="font-family:arial,helvetica,sans-serif; height:2px; width:700px;">
<table cellpadding="3" cellspacing="0" style="font-family:arial,helvetica,sans-serif; height:2px; width:800px;">
<tbody>
<tr>
<td style="background-color:{{ $color }}; width:700px; text-align:center;"><span style="font-size:12px; color:#fff;"><strong>{{ .Status }}</strong></span></td>
<td style="background-color:{{ $color }}; width:800px; text-align:center;"><span style="font-size:12px; color:#fff;"><strong>{{ .Status }}</strong></span></td>
</tr>
</tbody>
</table>
<table cellpadding="5" cellspacing="0" style="font-family:arial,helvetica,sans-serif; width:700px; font-size:13px">
<table cellpadding="5" cellspacing="0" style="font-family:arial,helvetica,sans-serif; width:800px; font-size:13px">
<tbody>
<tr>
<td style="background-color:#858585"><span style="font-size:14px;color:#fff;"><strong>Action</strong></span></td>
Expand Down Expand Up @@ -97,7 +97,7 @@ var htmlTmpl = `
{{ if .Output }}
<tr>
<td style="background-color:#858585"><span style="font-size:14px;color:#fff;"><strong>Output</strong></span></td>
<td style="background-color:#d1d6da">{{ printf "%s" .Output }}</td>
<td style="background-color:#d1d6da;max-width:502px;overflow:hidden;">{{ printf "%s" .Output }}</td>
</tr>
{{ end }}
</tbody>
Expand Down

0 comments on commit c92d8a3

Please sign in to comment.