Skip to content

Commit

Permalink
conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
ishustava committed Apr 9, 2020
1 parent 724a532 commit 4a33ffa
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 23 deletions.
60 changes: 37 additions & 23 deletions subcommand/server-acl-init/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
k8sflags "github.com/hashicorp/consul-k8s/subcommand/flags"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/go-discover"
"github.com/hashicorp/go-hclog"
"github.com/mitchellh/cli"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -25,26 +26,32 @@ import (
type Command struct {
UI cli.Ui

flags *flag.FlagSet
k8s *k8sflags.K8SFlags
flagResourcePrefix string
flagK8sNamespace string
flagAllowDNS bool
flagCreateClientToken bool
flagCreateSyncToken bool
flagCreateInjectToken bool
flagCreateInjectAuthMethod bool
flagBindingRuleSelector string
flagCreateEntLicenseToken bool
flagCreateSnapshotAgentToken bool
flagCreateMeshGatewayToken bool
flags *flag.FlagSet
k8s *k8sflags.K8SFlags

flagResourcePrefix string
flagK8sNamespace string

flagAllowDNS bool
flagCreateClientToken bool
flagCreateSyncToken bool
flagCreateInjectToken bool
flagCreateInjectAuthMethod bool
flagBindingRuleSelector string
flagCreateEntLicenseToken bool
flagCreateSnapshotAgentToken bool
flagCreateMeshGatewayToken bool

// Flags to configure Consul client
flagServerAddresses []string
flagServerPort uint
flagConsulCACert string
flagConsulTLSServerName string
flagUseHTTPS bool

// Flags for ACL replication
flagCreateACLReplicationToken bool
flagACLReplicationTokenFile string
flagConsulCACert string
flagConsulTLSServerName string
flagUseHTTPS bool
flagServerAddresses []string
flagServerPort uint

// Flags to support namespaces
flagEnableNamespaces bool // Use namespacing on all components
Expand All @@ -68,15 +75,14 @@ type Command struct {

once sync.Once
help string

providers map[string]discover.Provider
}

func (c *Command) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.flags.StringVar(&c.flagResourcePrefix, "resource-prefix", "",
"Prefix to use for Kubernetes resources. If not set, the \"<release-name>-consul\" prefix is used, where <release-name> is the value set by the -release-name flag.")
c.flags.Var((*flags.AppendSliceValue)(&c.flagServerAddresses), "server-address",
"The IP or DNS name of the Consul server(s), may be provided multiple times. At least one value is required.")
c.flags.UintVar(&c.flagServerPort, "server-port", 8500, "The HTTP or HTTPS port of the Consul server. Defaults to 8500.")
c.flags.StringVar(&c.flagK8sNamespace, "k8s-namespace", "",
"Name of Kubernetes namespace where the servers are deployed")
c.flags.BoolVar(&c.flagAllowDNS, "allow-dns", false,
Expand All @@ -99,14 +105,18 @@ func (c *Command) init() {
"Toggle for creating a token for the Consul snapshot agent deployment (enterprise only)")
c.flags.BoolVar(&c.flagCreateMeshGatewayToken, "create-mesh-gateway-token", false,
"Toggle for creating a token for a Connect mesh gateway")
c.flags.BoolVar(&c.flagCreateACLReplicationToken, "create-acl-replication-token", false,
"Toggle for creating a token for ACL replication between datacenters")

c.flags.Var((*flags.AppendSliceValue)(&c.flagServerAddresses), "server-address",
"The IP, DNS name or cloud auto-join string of the Consul server(s), may be provided multiple times." +
"At least one value is required.")
c.flags.UintVar(&c.flagServerPort, "server-port", 8500, "The HTTP or HTTPS port of the Consul server. Defaults to 8500.")
c.flags.StringVar(&c.flagConsulCACert, "consul-ca-cert", "",
"Path to the PEM-encoded CA certificate of the Consul cluster.")
c.flags.StringVar(&c.flagConsulTLSServerName, "consul-tls-server-name", "",
"The server name to set as the SNI header when sending HTTPS requests to Consul.")
c.flags.BoolVar(&c.flagUseHTTPS, "use-https", false,
"Toggle for using HTTPS for all API calls to Consul.")

c.flags.BoolVar(&c.flagEnableNamespaces, "enable-namespaces", false,
"[Enterprise Only] Enables namespaces, in either a single Consul namespace or mirrored [Enterprise only feature]")
c.flags.StringVar(&c.flagConsulSyncDestinationNamespace, "consul-sync-destination-namespace", "default",
Expand All @@ -125,6 +135,9 @@ func (c *Command) init() {
c.flags.StringVar(&c.flagInjectK8SNSMirroringPrefix, "inject-k8s-namespace-mirroring-prefix", "",
"[Enterprise Only] Prefix that will be added to all k8s namespaces mirrored into Consul by Connect inject "+
"if mirroring is enabled.")

c.flags.BoolVar(&c.flagCreateACLReplicationToken, "create-acl-replication-token", false,
"Toggle for creating a token for ACL replication between datacenters")
c.flags.StringVar(&c.flagACLReplicationTokenFile, "acl-replication-token-file", "",
"Path to file containing ACL token to be used for ACL replication. If set, ACL replication is enabled.")
c.flags.DurationVar(&c.flagTimeout, "timeout", 10*time.Minute,
Expand Down Expand Up @@ -171,6 +184,7 @@ func (c *Command) Run(args []string) int {
c.UI.Error("-resource-prefix must be set")
return 1
}

var aclReplicationToken string
if c.flagACLReplicationTokenFile != "" {
// Load the ACL replication token from file.
Expand Down
58 changes: 58 additions & 0 deletions subcommand/server-acl-init/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/http/httptest"
Expand All @@ -19,6 +20,7 @@ import (
"github.com/hashicorp/consul/sdk/freeport"
"github.com/hashicorp/consul/sdk/testutil"
"github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/hashicorp/go-discover"
"github.com/mitchellh/cli"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -1171,6 +1173,49 @@ func TestRun_AnonPolicy_IgnoredWithReplication(t *testing.T) {
}
}

func TestRun_DefaultWithCloudAutoJoin(t *testing.T) {
t.Parallel()

k8s, testSvr := completeSetup(t)
defer testSvr.Stop()
require := require.New(t)

provider := &fakeProvider{}
// Run the command.
ui := cli.NewMockUi()
cmd := Command{
UI: ui,
clientset: k8s,
providers: map[string]discover.Provider{"fake": provider},
}
args := []string{
"-k8s-namespace=" + ns,
"-resource-prefix=" + resourcePrefix,
"-server-address", "provider=fake address=127.0.0.1",
"-server-port", strings.Split(testSvr.HTTPAddr, ":")[1],
}
responseCode := cmd.Run(args)
require.Equal(0, responseCode, ui.ErrorWriter.String())

// Test that the bootstrap kube secret is created.
bootToken := getBootToken(t, k8s, resourcePrefix, ns)

// Check that it has the right policies.
consul, err := api.NewClient(&api.Config{
Address: testSvr.HTTPAddr,
Token: bootToken,
})
require.NoError(err)
tokenData, _, err := consul.ACL().TokenReadSelf(nil)
require.NoError(err)
require.Equal("global-management", tokenData.Policies[0].Name)

// Check that the agent policy was created.
agentPolicy := policyExists(t, "agent-token", consul)
// Should be a global policy.
require.Len(agentPolicy.Datacenters, 0)
}

// Set up test consul agent and kubernetes cluster.
func completeSetup(t *testing.T) (*fake.Clientset, *testutil.TestServer) {
k8s := fake.NewSimpleClientset()
Expand Down Expand Up @@ -1425,5 +1470,18 @@ func writeTempFile(t *testing.T, contents string) (string, func()) {
}
}

type fakeProvider struct {
addrsNumCalls int
}

func (p *fakeProvider) Addrs(args map[string]string, l *log.Logger) ([]string, error) {
p.addrsNumCalls++
return []string{args["address"]}, nil
}

func (p *fakeProvider) Help() string {
return "fake-provider help"
}

var serviceAccountCACert = "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURDekNDQWZPZ0F3SUJBZ0lRS3pzN05qbDlIczZYYzhFWG91MjVoekFOQmdrcWhraUc5dzBCQVFzRkFEQXYKTVMwd0t3WURWUVFERXlRMU9XVTJaR00wTVMweU1EaG1MVFF3T1RVdFlUSTRPUzB4Wm1NM01EQmhZekZqWXpndwpIaGNOTVRrd05qQTNNVEF4TnpNeFdoY05NalF3TmpBMU1URXhOek14V2pBdk1TMHdLd1lEVlFRREV5UTFPV1UyClpHTTBNUzB5TURobUxUUXdPVFV0WVRJNE9TMHhabU0zTURCaFl6RmpZemd3Z2dFaU1BMEdDU3FHU0liM0RRRUIKQVFVQUE0SUJEd0F3Z2dFS0FvSUJBUURaakh6d3FvZnpUcEdwYzBNZElDUzdldXZmdWpVS0UzUEMvYXBmREFnQgo0anpFRktBNzgvOStLVUd3L2MvMFNIZVN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