-
Notifications
You must be signed in to change notification settings - Fork 1
/
attest.go
87 lines (73 loc) · 2.25 KB
/
attest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package cli
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"runtime"
"github.com/immune-gmbh/agent/v3/pkg/api"
"github.com/immune-gmbh/agent/v3/pkg/core"
"github.com/immune-gmbh/agent/v3/pkg/ipc"
"github.com/immune-gmbh/agent/v3/pkg/tui"
"github.com/rs/zerolog/log"
)
type attestCmd struct {
DryRun bool `name:"dry-run" help:"Do full attest but don't contact the immune servers" default:"false"`
Dump string `optional:"" name:"dump-report" help:"Specify a file to dump the security report to" type:"path"`
Standalone bool `help:"Don't connect to windows service to run attest"`
}
func (attest *attestCmd) winSvcAttest(ctx context.Context, stdLogOut io.Writer) error {
client, _, err := ipc.ConnectNamedPipe(ctx, stdLogOut)
if err != nil {
log.Error().Err(err).Msg("failed to connect to server")
return err
}
defer client.Shutdown()
if reply, err := client.Attest(ipc.CmdArgsAttest{DryRun: attest.DryRun}); err != nil {
log.Error().Err(err).Msg("failed to attest on remote server")
return err
} else if len(reply.Status) > 0 {
return core.AttestationClientError(reply.Status)
}
return nil
}
func (attest *attestCmd) Run(agentCore *core.AttestationClient, stdLogOut *io.Writer) error {
ctx := context.Background()
runSvcClient := runtime.GOOS == "windows" && !attest.Standalone
var err error
var evidence *api.Evidence
if runSvcClient {
err = attest.winSvcAttest(ctx, *stdLogOut)
} else {
if !agentCore.State.IsEnrolled() {
log.Error().Msg("No previous state found, please enroll first.")
return errors.New("no-state")
}
evidence, err = agentCore.Attest(ctx, attest.DryRun)
}
if err != nil {
core.LogAttestErrors(&log.Logger, err)
tui.SetUIState(tui.StAttestationFailed)
return err
}
if !runSvcClient && attest.Dump != "" && evidence != nil {
evidenceJSON, err := json.Marshal(evidence)
if err != nil {
log.Debug().Err(err).Msg("json.Marshal(Evidence)")
log.Error().Msg("Failed to dump report.")
return err
}
if attest.Dump == "-" {
fmt.Println(string(evidenceJSON))
} else {
path := attest.Dump + ".evidence.json"
if err := os.WriteFile(path, evidenceJSON, 0644); err != nil {
return err
}
log.Info().Msgf("Dumped evidence json: %s", path)
}
}
return nil
}