-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
connect: include optional partition prefixes in SPIFFE identifiers (#…
…10507) NOTE: this does not include any intentions enforcement changes yet
- Loading branch information
Showing
18 changed files
with
275 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:feature | ||
connect: include optional partition prefixes in SPIFFE identifiers | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
package connect | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/hashicorp/consul/agent/structs" | ||
) | ||
|
||
// SpiffeIDService is the structure to represent the SPIFFE ID for an agent. | ||
type SpiffeIDAgent struct { | ||
Host string | ||
Partition string | ||
Datacenter string | ||
Agent string | ||
} | ||
|
||
func (id SpiffeIDAgent) PartitionOrDefault() string { | ||
return structs.PartitionOrDefault(id.Partition) | ||
} | ||
|
||
// URI returns the *url.URL for this SPIFFE ID. | ||
func (id *SpiffeIDAgent) URI() *url.URL { | ||
func (id SpiffeIDAgent) URI() *url.URL { | ||
var result url.URL | ||
result.Scheme = "spiffe" | ||
result.Host = id.Host | ||
result.Path = fmt.Sprintf("/agent/client/dc/%s/id/%s", id.Datacenter, id.Agent) | ||
result.Path = id.uriPath() | ||
return &result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// +build !consulent | ||
|
||
package connect | ||
|
||
import "fmt" | ||
|
||
func (id SpiffeIDAgent) uriPath() string { | ||
return fmt.Sprintf("/agent/client/dc/%s/id/%s", id.Datacenter, id.Agent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// +build !consulent | ||
|
||
package connect | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSpiffeIDAgentURI(t *testing.T) { | ||
t.Run("default partition", func(t *testing.T) { | ||
agent := &SpiffeIDAgent{ | ||
Host: "1234.consul", | ||
Datacenter: "dc1", | ||
Agent: "123", | ||
} | ||
|
||
require.Equal(t, "spiffe://1234.consul/agent/client/dc/dc1/id/123", agent.URI().String()) | ||
}) | ||
|
||
t.Run("partitions are ignored", func(t *testing.T) { | ||
agent := &SpiffeIDAgent{ | ||
Host: "1234.consul", | ||
Partition: "foobar", | ||
Datacenter: "dc1", | ||
Agent: "123", | ||
} | ||
|
||
require.Equal(t, "spiffe://1234.consul/agent/client/dc/dc1/id/123", agent.URI().String()) | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,37 @@ | ||
package connect | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/hashicorp/consul/agent/structs" | ||
) | ||
|
||
// SpiffeIDService is the structure to represent the SPIFFE ID for a service. | ||
type SpiffeIDService struct { | ||
Host string | ||
Partition string | ||
Namespace string | ||
Datacenter string | ||
Service string | ||
} | ||
|
||
func (id SpiffeIDService) NamespaceOrDefault() string { | ||
return structs.NamespaceOrDefault(id.Namespace) | ||
} | ||
|
||
func (id SpiffeIDService) MatchesPartition(partition string) bool { | ||
return id.PartitionOrDefault() == structs.PartitionOrDefault(partition) | ||
} | ||
|
||
func (id SpiffeIDService) PartitionOrDefault() string { | ||
return structs.PartitionOrDefault(id.Partition) | ||
} | ||
|
||
// URI returns the *url.URL for this SPIFFE ID. | ||
func (id *SpiffeIDService) URI() *url.URL { | ||
func (id SpiffeIDService) URI() *url.URL { | ||
var result url.URL | ||
result.Scheme = "spiffe" | ||
result.Host = id.Host | ||
result.Path = fmt.Sprintf("/ns/%s/dc/%s/svc/%s", | ||
id.Namespace, id.Datacenter, id.Service) | ||
result.Path = id.uriPath() | ||
return &result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// +build !consulent | ||
|
||
package connect | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSpiffeIDServiceURI(t *testing.T) { | ||
t.Run("default partition; default namespace", func(t *testing.T) { | ||
svc := &SpiffeIDService{ | ||
Host: "1234.consul", | ||
Datacenter: "dc1", | ||
Service: "web", | ||
} | ||
require.Equal(t, "spiffe://1234.consul/ns/default/dc/dc1/svc/web", svc.URI().String()) | ||
}) | ||
|
||
t.Run("partitions are ignored", func(t *testing.T) { | ||
svc := &SpiffeIDService{ | ||
Host: "1234.consul", | ||
Partition: "other", | ||
Datacenter: "dc1", | ||
Service: "web", | ||
} | ||
require.Equal(t, "spiffe://1234.consul/ns/default/dc/dc1/svc/web", svc.URI().String()) | ||
}) | ||
|
||
t.Run("namespaces are ignored", func(t *testing.T) { | ||
svc := &SpiffeIDService{ | ||
Host: "1234.consul", | ||
Namespace: "other", | ||
Datacenter: "dc1", | ||
Service: "web", | ||
} | ||
require.Equal(t, "spiffe://1234.consul/ns/default/dc/dc1/svc/web", svc.URI().String()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.