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

Add http node attestor #4909

Merged
merged 56 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
b322773
Add http challenge node attestor
kfox1111 May 10, 2024
f08d9d2
Fix various issues so it works again after refactor
kfox1111 May 11, 2024
009166f
Fix some issues
kfox1111 May 11, 2024
7caac6f
Fix some issues
kfox1111 May 11, 2024
3e68d07
Fix some issues
kfox1111 May 11, 2024
baaa4f6
Fix some issues
kfox1111 May 11, 2024
029f117
Fix some issues
kfox1111 May 11, 2024
82af10b
Fix some issues
kfox1111 May 11, 2024
86fbf7c
Fix some issues
kfox1111 May 11, 2024
727c5f9
Implement tofu. Incorperate feedback
kfox1111 May 15, 2024
9f42248
Fix some lint bits
kfox1111 May 16, 2024
76d761f
More lint
kfox1111 May 16, 2024
a693947
More lint
kfox1111 May 16, 2024
28dc797
Merge branch 'main' into http
kfox1111 May 16, 2024
6416f01
Merge branch 'main' into http
kfox1111 May 30, 2024
9e8897e
Apply suggestions from code review
kfox1111 Jun 5, 2024
fdfc837
Incorperate feedback
kfox1111 Jun 5, 2024
f8bc768
Merge branch 'http' of https://github.com/kfox1111/spire into http
kfox1111 Jun 5, 2024
b5a439e
Incorperate feedback
kfox1111 Jun 5, 2024
0656513
Incorperate feedback
kfox1111 Jun 5, 2024
6a94dfd
Fix example
kfox1111 Jun 5, 2024
4e8133e
Incorperate feedback
kfox1111 Jun 10, 2024
b0df1d6
Incorperate feedback
kfox1111 Jun 10, 2024
0c1d254
Incorperate feedback
kfox1111 Jun 11, 2024
effb6b3
Fix lint issues
kfox1111 Jun 12, 2024
d6f3b6a
Fix lint issues
kfox1111 Jun 12, 2024
6cdb65b
Fix 404 issue
kfox1111 Jun 17, 2024
98d7363
Fix lint issue
kfox1111 Jun 17, 2024
623ed12
Fix issue with protocol
kfox1111 Jun 18, 2024
968e90e
Merge branch 'main' into http
kfox1111 Jun 18, 2024
0e7dd47
Remove tempate
kfox1111 Jun 22, 2024
b687049
Apply suggestions from code review
kfox1111 Jun 22, 2024
1caaaec
Merge branch 'main' into http
kfox1111 Jun 22, 2024
069bb2b
Fix typo
kfox1111 Jun 22, 2024
69edbe1
Add some unit tests
kfox1111 Jun 24, 2024
41f1e74
Add a bunch of httpchallenge server plugin tests
kfox1111 Jul 16, 2024
92e72d5
Add some more tests
kfox1111 Jul 16, 2024
151d3c3
Test server works
kfox1111 Jul 17, 2024
b6c4a41
Add tofu checks
kfox1111 Jul 17, 2024
f00d02f
Add httpchallenge agent tests
kfox1111 Jul 19, 2024
81b205a
Incorperate feedback
kfox1111 Jul 19, 2024
501a957
gofmt files
kfox1111 Jul 19, 2024
a404aa3
Apply suggestions from code review
kfox1111 Jul 19, 2024
5ab497d
Fix lint issues
kfox1111 Jul 19, 2024
b5caee3
Fix lint issues
kfox1111 Jul 19, 2024
1af2187
Fix test
kfox1111 Jul 19, 2024
a2c0385
Incorperate feedback. Add localhost block.
kfox1111 Jul 20, 2024
be2b414
Fix test
kfox1111 Jul 20, 2024
2bec5df
Add test for localhost test. Incorperate feedback
kfox1111 Jul 20, 2024
4c95d25
Incorperate feedback
kfox1111 Jul 20, 2024
2ac078f
Fix lint issue
kfox1111 Jul 20, 2024
32f8f04
Merge branch 'main' into http
kfox1111 Jul 20, 2024
f47af5c
Merge branch 'main' into http
kfox1111 Jul 23, 2024
f596796
Update doc/plugin_server_nodeattestor_http_challenge.md
kfox1111 Aug 5, 2024
2880654
Merge branch 'main' into http
kfox1111 Aug 5, 2024
3dae7a2
Incorperate feedback
kfox1111 Aug 5, 2024
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
Prev Previous commit
Next Next commit
Incorperate feedback. Add localhost block.
Signed-off-by: Kevin Fox <Kevin.Fox@pnnl.gov>
  • Loading branch information
kfox1111 committed Jul 20, 2024
commit a2c0385a55f23e2fd40db953fd68f241f06ce5fc
26 changes: 19 additions & 7 deletions pkg/agent/plugin/nodeattestor/httpchallenge/httpchallenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ func BuiltIn() catalog.BuiltIn {
return builtin(New())
}

func BuiltInWithHostname(hostname string) catalog.BuiltIn {
plugin := New()
plugin.hostname = hostname
return builtin(plugin)
}

func builtin(p *Plugin) catalog.BuiltIn {
return catalog.MakeBuiltIn(pluginName,
nodeattestorv1.NodeAttestorPluginServer(p),
Expand Down Expand Up @@ -56,6 +62,8 @@ type Plugin struct {
c *Config

log hclog.Logger

hostname string
}

func New() *Plugin {
Expand Down Expand Up @@ -137,7 +145,7 @@ func (p *Plugin) Configure(_ context.Context, req *configv1.ConfigureRequest) (*
}

// Make sure the configuration produces valid data
if _, err := loadConfigData(config); err != nil {
if _, err := loadConfigData(p.hostname, config); err != nil {
return nil, err
}

Expand Down Expand Up @@ -193,15 +201,19 @@ func (p *Plugin) loadConfigData() (*configData, error) {
if config == nil {
return nil, status.Error(codes.FailedPrecondition, "not configured")
}
return loadConfigData(config)
return loadConfigData(p.hostname, config)
}

func loadConfigData(config *Config) (*configData, error) {
func loadConfigData(hostname string, config *Config) (*configData, error) {
if config.HostName == "" {
var err error
config.HostName, err = os.Hostname()
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "unable to fetch hostname: %v", err)
if hostname != "" {
config.HostName = hostname
} else {
var err error
config.HostName, err = os.Hostname()
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "unable to fetch hostname: %v", err)
}
}
}
var agentName = "default"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"net"
"net/http"
"testing"

configv1 "github.com/spiffe/spire-plugin-sdk/proto/spire/service/common/config/v1"
Expand Down Expand Up @@ -155,7 +156,7 @@ func TestAidAttestationSucceeds(t *testing.T) {
serverStream: func(attestationData *common_httpchallenge.AttestationData, challenge []byte, expectPayload []byte, challengeobj *common_httpchallenge.Challenge, port int) nodeattestor.ServerStream {
return streamBuilder.IgnoreThenChallenge(challenge).
Handle(func(challengeResponse []byte) ([]byte, error) {
err := common_httpchallenge.VerifyChallenge(attestationData, challengeobj)
err := common_httpchallenge.VerifyChallenge(http.DefaultClient, attestationData, challengeobj)
return nil, err
}).Build()
},
Expand All @@ -171,7 +172,7 @@ func TestAidAttestationSucceeds(t *testing.T) {
serverStream: func(attestationData *common_httpchallenge.AttestationData, challenge []byte, expectPayload []byte, challengeobj *common_httpchallenge.Challenge, port int) nodeattestor.ServerStream {
return streamBuilder.ExpectThenChallenge(expectPayload, challenge).
Handle(func(challengeResponse []byte) ([]byte, error) {
err := common_httpchallenge.VerifyChallenge(attestationData, challengeobj)
err := common_httpchallenge.VerifyChallenge(http.DefaultClient, attestationData, challengeobj)
return nil, err
}).Build()
},
Expand Down Expand Up @@ -205,6 +206,6 @@ func loadAndConfigurePlugin(t *testing.T, config string) nodeattestor.NodeAttest

func loadPlugin(t *testing.T, options ...plugintest.Option) nodeattestor.NodeAttestor {
na := new(nodeattestor.V1)
plugintest.Load(t, httpchallenge.BuiltIn(), na, options...)
plugintest.Load(t, httpchallenge.BuiltInWithHostname("wark"), na, options...)
return na
}
3 changes: 1 addition & 2 deletions pkg/common/plugin/httpchallenge/httpchallenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func CalculateResponse(_ *Challenge) (*Response, error) {
return &Response{}, nil
}

func VerifyChallenge(attestationData *AttestationData, challenge *Challenge) error {
func VerifyChallenge(client *http.Client, attestationData *AttestationData, challenge *Challenge) error {
if attestationData.HostName == "" {
return fmt.Errorf("hostname must be set")
}
Expand Down Expand Up @@ -86,7 +86,6 @@ func VerifyChallenge(attestationData *AttestationData, challenge *Challenge) err
return err
}

client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/plugin/httpchallenge/httpchallenge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestValidateChallenge(t *testing.T) {
return dialer.DialContext(ctx, network, addr)
}

err := VerifyChallenge(ad, c)
err := VerifyChallenge(http.DefaultClient, ad, c)
kfox1111 marked this conversation as resolved.
Show resolved Hide resolved
if tt.expectErr != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tt.expectErr)
Expand Down
12 changes: 10 additions & 2 deletions pkg/server/plugin/nodeattestor/httpchallenge/httpchallenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httpchallenge
import (
"context"
"encoding/json"
"net/http"
"regexp"
"sync"

Expand Down Expand Up @@ -58,10 +59,14 @@ type Plugin struct {
config *configuration

log hclog.Logger

client *http.Client
}

func New() *Plugin {
return &Plugin{}
return &Plugin{
client: http.DefaultClient,
}
}

func (p *Plugin) Attest(stream nodeattestorv1.NodeAttestor_AttestServer) error {
Expand Down Expand Up @@ -125,7 +130,7 @@ func (p *Plugin) Attest(stream nodeattestorv1.NodeAttestor_AttestServer) error {
}

p.log.Debug("Verifying challenge")
if err := httpchallenge.VerifyChallenge(attestationData, challenge); err != nil {
if err := httpchallenge.VerifyChallenge(p.client, attestationData, challenge); err != nil {
return status.Errorf(codes.PermissionDenied, "challenge verification failed: %v", err)
}

Expand Down Expand Up @@ -254,6 +259,9 @@ func validateAgentName(agentName string, agentNamePattern *regexp.Regexp) error
}

func validateHostName(hostName string, dnsPatterns []*regexp.Regexp) error {
if hostName == "localhost" {
return status.Errorf(codes.PermissionDenied, "you can not use localhost as a hostname")
}
if len(dnsPatterns) == 0 {
return nil
}
Expand Down
Loading