/
sat.go
139 lines (115 loc) · 3.44 KB
/
sat.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package k8ssat
import (
"context"
"encoding/json"
"fmt"
"os"
"sync"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/hcl"
nodeattestorv1 "github.com/spiffe/spire-plugin-sdk/proto/spire/plugin/agent/nodeattestor/v1"
configv1 "github.com/spiffe/spire-plugin-sdk/proto/spire/service/common/config/v1"
"github.com/spiffe/spire/pkg/common/catalog"
"github.com/spiffe/spire/pkg/common/plugin/k8s"
"github.com/zeebo/errs"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
pluginName = "k8s_sat"
defaultTokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token" //nolint: gosec // false positive
)
func BuiltIn() catalog.BuiltIn {
return builtin(New())
}
func builtin(p *AttestorPlugin) catalog.BuiltIn {
return catalog.MakeBuiltIn(pluginName,
nodeattestorv1.NodeAttestorPluginServer(p),
configv1.ConfigServiceServer(p))
}
type AttestorConfig struct {
Cluster string `hcl:"cluster"`
TokenPath string `hcl:"token_path"`
}
type attestorConfig struct {
cluster string
tokenPath string
}
type AttestorPlugin struct {
nodeattestorv1.UnsafeNodeAttestorServer
configv1.UnsafeConfigServer
log hclog.Logger
mu sync.RWMutex
config *attestorConfig
}
func New() *AttestorPlugin {
return &AttestorPlugin{}
}
// SetLogger sets a logger in the plugin.
func (p *AttestorPlugin) SetLogger(log hclog.Logger) {
p.log = log
}
func (p *AttestorPlugin) AidAttestation(stream nodeattestorv1.NodeAttestor_AidAttestationServer) error {
config, err := p.getConfig()
if err != nil {
return err
}
token, err := loadTokenFromFile(config.tokenPath)
if err != nil {
return status.Errorf(codes.InvalidArgument, "unable to load token from %s: %v", config.tokenPath, err)
}
payload, err := json.Marshal(k8s.SATAttestationData{
Cluster: config.cluster,
Token: token,
})
if err != nil {
return status.Errorf(codes.Internal, "unable to marshal SAT token data: %v", err)
}
return stream.Send(&nodeattestorv1.PayloadOrChallengeResponse{
Data: &nodeattestorv1.PayloadOrChallengeResponse_Payload{
Payload: payload,
},
})
}
func (p *AttestorPlugin) Configure(_ context.Context, req *configv1.ConfigureRequest) (resp *configv1.ConfigureResponse, err error) {
p.log.Warn(fmt.Sprintf("The %q node attestor plugin has been deprecated in favor of the \"k8s_psat\" plugin and will be removed in a future release", pluginName))
hclConfig := new(AttestorConfig)
if err := hcl.Decode(hclConfig, req.HclConfiguration); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "unable to decode configuration: %v", err)
}
if hclConfig.Cluster == "" {
return nil, status.Error(codes.InvalidArgument, "configuration missing cluster")
}
config := &attestorConfig{
cluster: hclConfig.Cluster,
tokenPath: hclConfig.TokenPath,
}
if config.tokenPath == "" {
config.tokenPath = getDefaultTokenPath()
}
p.setConfig(config)
return &configv1.ConfigureResponse{}, nil
}
func (p *AttestorPlugin) getConfig() (*attestorConfig, error) {
p.mu.RLock()
defer p.mu.RUnlock()
if p.config == nil {
return nil, status.Error(codes.FailedPrecondition, "not configured")
}
return p.config, nil
}
func (p *AttestorPlugin) setConfig(config *attestorConfig) {
p.mu.Lock()
defer p.mu.Unlock()
p.config = config
}
func loadTokenFromFile(path string) (string, error) {
data, err := os.ReadFile(path)
if err != nil {
return "", errs.Wrap(err)
}
if len(data) == 0 {
return "", errs.New("%q is empty", path)
}
return string(data), nil
}