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
Incorperate feedback
Signed-off-by: Kevin Fox <Kevin.Fox@pnnl.gov>
  • Loading branch information
kfox1111 committed Aug 5, 2024
commit 3dae7a2ef2cd9c1f8ee9670a6d611529264c0917
Original file line number Diff line number Diff line change
Expand Up @@ -156,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(http.DefaultClient, attestationData, challengeobj)
err := common_httpchallenge.VerifyChallenge(context.Background(), http.DefaultClient, attestationData, challengeobj)
return nil, err
}).Build()
},
Expand All @@ -172,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(http.DefaultClient, attestationData, challengeobj)
err := common_httpchallenge.VerifyChallenge(context.Background(), http.DefaultClient, attestationData, challengeobj)
return nil, err
}).Build()
},
Expand Down
6 changes: 1 addition & 5 deletions pkg/common/plugin/httpchallenge/httpchallenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"net/url"
"strconv"
"strings"
"time"

"github.com/spiffe/go-spiffe/v2/spiffeid"
"github.com/spiffe/spire/pkg/common/idutil"
Expand Down Expand Up @@ -53,7 +52,7 @@ func CalculateResponse(_ *Challenge) (*Response, error) {
return &Response{}, nil
}

func VerifyChallenge(client *http.Client, attestationData *AttestationData, challenge *Challenge) error {
func VerifyChallenge(ctx context.Context, client *http.Client, attestationData *AttestationData, challenge *Challenge) error {
if attestationData.HostName == "" {
return fmt.Errorf("hostname must be set")
}
Expand All @@ -78,9 +77,6 @@ func VerifyChallenge(client *http.Client, attestationData *AttestationData, chal
Path: fmt.Sprintf("/.well-known/spiffe/nodeattestor/http_challenge/%s/challenge", attestationData.AgentName),
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

req, err := http.NewRequestWithContext(ctx, "GET", turl.String(), nil)
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions pkg/common/plugin/httpchallenge/httpchallenge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,16 @@ func TestValidateChallenge(t *testing.T) {
}))
defer func() { testServer.Close() }()

http.DefaultTransport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
if addr == "foo.bar:80" {
addr = strings.TrimPrefix(testServer.URL, "http://")
}
dialer := &net.Dialer{}
return dialer.DialContext(ctx, network, addr)
}

err := VerifyChallenge(http.DefaultClient, ad, c)
err := VerifyChallenge(context.Background(), &http.Client{Transport: transport}, ad, c)
if tt.expectErr != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tt.expectErr)
Expand Down
19 changes: 12 additions & 7 deletions pkg/server/plugin/nodeattestor/httpchallenge/httpchallenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"regexp"
"sync"
"time"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/hcl"
Expand All @@ -23,6 +24,10 @@ const (
pluginName = "http_challenge"
)

var (
agentNamePattern = regexp.MustCompile("^[a-zA-z]+[a-zA-Z0-9-]$")
)

func BuiltIn() catalog.BuiltIn {
return builtin(New())
}
Expand All @@ -46,7 +51,6 @@ type configuration struct {
requiredPort *int
allowNonRootPorts bool
dnsPatterns []*regexp.Regexp
agentNamePattern *regexp.Regexp
tofu bool
}

Expand Down Expand Up @@ -105,7 +109,7 @@ func (p *Plugin) Attest(stream nodeattestorv1.NodeAttestor_AttestServer) error {
return status.Errorf(codes.InvalidArgument, "port %d is not allowed to be >= 1024", attestationData.Port)
}

if err = validateAgentName(attestationData.AgentName, config.agentNamePattern); err != nil {
if err = validateAgentName(attestationData.AgentName); err != nil {
return err
}

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

p.log.Debug("Verifying challenge")
if err := httpchallenge.VerifyChallenge(p.client, attestationData, challenge); err != nil {

timeoutctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

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

Expand Down Expand Up @@ -189,8 +197,6 @@ func (p *Plugin) Configure(_ context.Context, req *configv1.ConfigureRequest) (*
dnsPatterns = append(dnsPatterns, re)
}

agentNamePattern := regexp.MustCompile("^[a-zA-z]+[a-zA-Z0-9-]$")

allowNonRootPorts := true
if hclConfig.AllowNonRootPorts != nil {
allowNonRootPorts = *hclConfig.AllowNonRootPorts
Expand Down Expand Up @@ -223,7 +229,6 @@ func (p *Plugin) Configure(_ context.Context, req *configv1.ConfigureRequest) (*
dnsPatterns: dnsPatterns,
requiredPort: hclConfig.RequiredPort,
allowNonRootPorts: allowNonRootPorts,
agentNamePattern: agentNamePattern,
tofu: tofu,
})

Expand Down Expand Up @@ -258,7 +263,7 @@ func buildSelectorValues(hostName string) []string {
return selectorValues
}

func validateAgentName(agentName string, agentNamePattern *regexp.Regexp) error {
func validateAgentName(agentName string) error {
l := agentNamePattern.FindAllStringSubmatch(agentName, -1)
if len(l) != 1 || len(l[0]) == 0 || len(l[0]) > 32 {
return status.Error(codes.InvalidArgument, "agent name is not valid")
Expand Down
Loading