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

ACL token passing integration test #272

Merged
merged 1 commit into from
Oct 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
48 changes: 48 additions & 0 deletions consul/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1057,3 +1057,51 @@ func Test_substituteEnvironment(t *testing.T) {
})
}
}

func TestTokenIsUsedToConnectToConsul(t *testing.T) {
// given
t.Parallel()
server := CreateSecuredTestServer(t)
defer server.Stop()

bareClient := ClientAtServer(server)
bareClient.config.Tag = "marathon"

clientWithToken := SecuredClientAtServer(server)
clientWithToken.config.Tag = "marathon"

// and
app := utils.ConsulApp("serviceA", 1)
app.Tasks[0].Host = server.Config.Bind
app.Labels["test"] = "tag"

// when
err := bareClient.Register(&app.Tasks[0], app)

// then
assert.NoError(t, err, "Though it seems surprising, consul should not report an error here")

// when
services, _ := clientWithToken.GetAllServices()

// then
assert.Len(t, services, 0, "Registration without ACL token should be blocked by ACLs")

// when
err = clientWithToken.Register(&app.Tasks[0], app)
assert.NoError(t, err, "Registering service with proper ACL token should not report errors")

// when
services, _ = clientWithToken.GetAllServices()

// then
assert.Len(t, services, 1, "Expecting a registered service after using ACL token")
assert.Equal(t, "serviceA", services[0].Name)
assert.Equal(t, []string{"marathon", "test", "marathon-task:serviceA.0"}, services[0].Tags)

// when
services, _ = bareClient.GetAllServices()

// then
assert.Len(t, services, 0, "Reading services list without ACL token should yield empty response")
}
56 changes: 47 additions & 9 deletions consul/consul_test_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,44 @@ import (
)

func CreateTestServer(t *testing.T) *testutil.TestServer {
ports, err := getPorts(6)
server, err := testutil.NewTestServerConfig(func(c *testutil.TestServerConfig) {
c.Datacenter = fmt.Sprint("dc-", time.Now().UnixNano())
c.Ports = testPortConfig(t)
})

assert.NoError(t, err)

return server
}

const MasterToken = "masterToken"

func CreateSecuredTestServer(t *testing.T) *testutil.TestServer {
server, err := testutil.NewTestServerConfig(func(c *testutil.TestServerConfig) {
c.Datacenter = fmt.Sprint("dc-", time.Now().UnixNano())
c.Ports = &testutil.TestPortConfig{
DNS: ports[0],
HTTP: ports[1],
RPC: ports[2],
SerfLan: ports[3],
SerfWan: ports[4],
Server: ports[5],
}
c.Ports = testPortConfig(t)
c.ACLDatacenter = c.Datacenter
c.ACLDefaultPolicy = "deny"
c.ACLMasterToken = MasterToken
})

assert.NoError(t, err)

return server
}
func testPortConfig(t *testing.T) *testutil.TestPortConfig {
ports, err := getPorts(6)
assert.NoError(t, err)

return &testutil.TestPortConfig{
DNS: ports[0],
HTTP: ports[1],
RPC: ports[2],
SerfLan: ports[3],
SerfWan: ports[4],
Server: ports[5],
}
}

// Ask the kernel for free open ports that are ready to use
func getPorts(number int) ([]int, error) {
Expand Down Expand Up @@ -61,6 +80,10 @@ func ClientAtServer(server *testutil.TestServer) *Consul {
return consulClientAtAddress(server.Config.Bind, server.Config.Ports.HTTP)
}

func SecuredClientAtServer(server *testutil.TestServer) *Consul {
return secureConsulClientAtAddress(server.Config.Bind, server.Config.Ports.HTTP)
}

func FailingClient() *Consul {
host, port := "192.0.2.5", 5555
config := Config{
Expand All @@ -87,3 +110,18 @@ func consulClientAtAddress(host string, port int) *Consul {
consul.AddAgent(host)
return consul
}

func secureConsulClientAtAddress(host string, port int) *Consul {
config := Config{
Timeout: timeutil.Interval{Duration: 10 * time.Second},
Port: fmt.Sprintf("%d", port),
ConsulNameSeparator: ".",
EnableTagOverride: true,
LocalAgentHost: host,
Token: MasterToken,
}
consul := New(config)
// initialize the agents cache with a single client pointing at provided location
consul.AddAgent(host)
return consul
}