-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added check if anonymous token policy exists (#2790)
* added check if anonymous token policy exists * changed checkIfAnonymousTokenPolicyExists impl * made consts private * added test for configureAnonymousPolicy * fixed unit test * fixed test and minor refactoring * fix typo * changed some var names * added changelog
- Loading branch information
Showing
3 changed files
with
103 additions
and
2 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:improvement | ||
control-plane: prevent updation of anonymous-token-policy and anonymous-token if anonymous-token-policy is already attached to the anonymous-token | ||
``` |
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
68 changes: 68 additions & 0 deletions
68
control-plane/subcommand/server-acl-init/anonymous_token_test.go
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,68 @@ | ||
package serveraclinit | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/consul/api" | ||
"github.com/mitchellh/cli" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_configureAnonymousPolicy(t *testing.T) { | ||
|
||
k8s, testClient := completeSetup(t) | ||
consulHTTPAddr := testClient.TestServer.HTTPAddr | ||
consulGRPCAddr := testClient.TestServer.GRPCAddr | ||
|
||
setUpK8sServiceAccount(t, k8s, ns) | ||
ui := cli.NewMockUi() | ||
cmd := Command{ | ||
UI: ui, | ||
clientset: k8s, | ||
} | ||
cmd.init() | ||
flags := []string{"-connect-inject"} | ||
cmdArgs := append([]string{ | ||
"-timeout=1m", | ||
"-resource-prefix=" + resourcePrefix, | ||
"-k8s-namespace=" + ns, | ||
"-auth-method-host=https://my-kube.com", | ||
"-addresses", strings.Split(consulHTTPAddr, ":")[0], | ||
"-http-port", strings.Split(consulHTTPAddr, ":")[1], | ||
"-grpc-port", strings.Split(consulGRPCAddr, ":")[1], | ||
}, flags...) | ||
responseCode := cmd.Run(cmdArgs) | ||
require.Equal(t, 0, responseCode, ui.ErrorWriter.String()) | ||
|
||
bootToken := getBootToken(t, k8s, resourcePrefix, ns) | ||
consul, err := api.NewClient(&api.Config{ | ||
Address: consulHTTPAddr, | ||
Token: bootToken, | ||
}) | ||
require.NoError(t, err) | ||
|
||
err = cmd.configureAnonymousPolicy(consul) | ||
require.NoError(t, err) | ||
|
||
policy, _, err := consul.ACL().PolicyReadByName(anonymousTokenPolicyName, nil) | ||
require.NoError(t, err) | ||
|
||
testPolicy := api.ACLPolicy{ | ||
ID: policy.ID, | ||
Name: anonymousTokenPolicyName, | ||
Description: "Anonymous token Policy", | ||
Rules: `acl = "read"`, | ||
} | ||
readOnlyPolicy, _, err := consul.ACL().PolicyUpdate(&testPolicy, &api.WriteOptions{}) | ||
require.NoError(t, err) | ||
|
||
err = cmd.configureAnonymousPolicy(consul) | ||
require.NoError(t, err) | ||
|
||
actualPolicy, _, err := consul.ACL().PolicyReadByName(anonymousTokenPolicyName, nil) | ||
require.NoError(t, err) | ||
|
||
// assert policy is still same. | ||
require.Equal(t, readOnlyPolicy, actualPolicy) | ||
} |