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

Adding support for AzureAD auth to AKS via RBAC #1820

Merged
merged 35 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5fabc0f
Adding support for AzureAD auth to AKS via RBAC
Aug 24, 2018
90909e8
Merge branch 'timcurless'
Aug 24, 2018
6b126fc
Merge branch 'master' into timcurless
timcurless Aug 24, 2018
74b118a
Merge branch 'timcurless' of https://github.com/timcurless/terraform-…
Aug 24, 2018
98dcb1b
formatting
Aug 24, 2018
f98d40b
formatting
Aug 24, 2018
7410d40
Merge branch 'master' into timcurless
timcurless Aug 31, 2018
2d45712
fixing conflicts
Aug 31, 2018
6afd99d
Merge branch 'master' into timcurless
Aug 31, 2018
68aa0f2
fixing conflicts
Aug 31, 2018
8b05407
Adding documentation updates
Aug 31, 2018
ef80ef0
Fixing issues with aadProfile server_app_secret always causing a new …
Sep 23, 2018
2db2c6d
Refactoring code to avoid repetition in some areas. Documentation upd…
Sep 29, 2018
aaab03f
minor updates to testing section of main documentation index
Oct 5, 2018
2d827a5
docs: client_id -> client_app_id
katbyte Oct 18, 2018
ed45f77
fixing one more misnamed var in the docs
Oct 19, 2018
655b079
Merge branch 'master' into timcurless
tombuildsstuff Nov 14, 2018
890333e
AKS: switching RBAC to be an independent block
tombuildsstuff Nov 14, 2018
a83c246
Rewriting the documentation to be clearer (ala VM's)
tombuildsstuff Nov 14, 2018
fb5b0cd
AKS: refactoring
tombuildsstuff Nov 14, 2018
bd61925
AKS: adding additional context to the errors
tombuildsstuff Nov 14, 2018
a5d1a4e
updating a todo
tombuildsstuff Nov 14, 2018
2025f17
Data Source: refactoring
tombuildsstuff Nov 14, 2018
12d2677
Data Source: continued re-ordering/removing the existing structure
tombuildsstuff Nov 14, 2018
1eb7ab4
d/AKS: adding the `role_based_access_control` block
tombuildsstuff Nov 14, 2018
c186e4c
AKS: support for up to 100 agents
tombuildsstuff Nov 14, 2018
7607444
Updating the versions of kubernetes being used
tombuildsstuff Nov 14, 2018
6fa3d6f
Documentation: moving the AKS Examples into their own folder
tombuildsstuff Nov 14, 2018
dabe1c9
removing the older aks examples
tombuildsstuff Nov 14, 2018
0ffad71
Updating to include the changes from #2286
tombuildsstuff Nov 14, 2018
3edc3a6
re-ordering/updating the errors
tombuildsstuff Nov 14, 2018
94196cd
removing the unused env variables
tombuildsstuff Nov 14, 2018
a8e97b5
fixing the linting
tombuildsstuff Nov 14, 2018
8b5965a
fixing broken tests
tombuildsstuff Nov 14, 2018
1773e86
fixes from code review
tombuildsstuff Nov 15, 2018
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
471 changes: 283 additions & 188 deletions azurerm/data_source_kubernetes_cluster.go

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions azurerm/data_source_kubernetes_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestAccDataSourceAzureRMKubernetesCluster_basic(t *testing.T) {
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKubernetesClusterExists(dataSourceName),
resource.TestCheckResourceAttr(dataSourceName, "role_based_access_control.#", "0"),
resource.TestCheckResourceAttrSet(dataSourceName, "kube_config.0.client_key"),
resource.TestCheckResourceAttrSet(dataSourceName, "kube_config.0.client_certificate"),
resource.TestCheckResourceAttrSet(dataSourceName, "kube_config.0.cluster_ca_certificate"),
Expand All @@ -38,6 +39,34 @@ func TestAccDataSourceAzureRMKubernetesCluster_basic(t *testing.T) {
})
}

func TestAccDataSourceAzureRMKubernetesCluster_roleBasedAccessControl(t *testing.T) {
dataSourceName := "data.azurerm_kubernetes_cluster.test"
ri := acctest.RandInt()
clientId := os.Getenv("ARM_CLIENT_ID")
clientSecret := os.Getenv("ARM_CLIENT_SECRET")
tenantId := os.Getenv("ARM_TENANT_ID")
location := testLocation()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKubernetesClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMKubernetesCluster_roleBasedAccessControl(ri, location, clientId, clientSecret, tenantId),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKubernetesClusterExists(dataSourceName),
resource.TestCheckResourceAttr(dataSourceName, "role_based_access_control.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "role_based_access_control.0.azure_active_directory.#", "1"),
resource.TestCheckResourceAttrSet(dataSourceName, "role_based_access_control.0.azure_active_directory.0.client_app_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "role_based_access_control.0.azure_active_directory.0.server_app_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "role_based_access_control.0.azure_active_directory.0.tenant_id"),
),
},
},
})
}

func TestAccDataSourceAzureRMKubernetesCluster_internalNetwork(t *testing.T) {
dataSourceName := "data.azurerm_kubernetes_cluster.test"
ri := acctest.RandInt()
Expand Down Expand Up @@ -244,6 +273,18 @@ data "azurerm_kubernetes_cluster" "test" {
`, r)
}

func testAccDataSourceAzureRMKubernetesCluster_roleBasedAccessControl(rInt int, location, clientId, clientSecret, tenantId string) string {
resource := testAccAzureRMKubernetesCluster_roleBasedAccessControl(rInt, location, clientId, clientSecret, tenantId)
return fmt.Sprintf(`
%s

data "azurerm_kubernetes_cluster" "test" {
name = "${azurerm_kubernetes_cluster.test.name}"
resource_group_name = "${azurerm_kubernetes_cluster.test.resource_group_name}"
}
`, resource)
}

func testAccDataSourceAzureRMKubernetesCluster_internalNetwork(rInt int, clientId string, clientSecret string, location string) string {
r := testAccAzureRMKubernetesCluster_internalNetwork(rInt, clientId, clientSecret, location)
return fmt.Sprintf(`
Expand Down
55 changes: 53 additions & 2 deletions azurerm/helpers/kubernetes/kube_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ type user struct {
ClientKeyData string `yaml:"client-key-data"`
}

type userItemAAD struct {
Name string `yaml:"name"`
User userAAD `yaml:"user"`
}

type userAAD struct {
AuthProvider authProvider `yaml:"auth-provider"`
}

type authProvider struct {
Name string `yaml:"name"`
Config configAzureAD `yaml:"config"`
}

type configAzureAD struct {
APIServerID string `yaml:"apiserver-id,omitempty"`
ClientID string `yaml:"client-id,omitempty"`
TenantID string `yaml:"tenant-id,omitempty"`
}

type contextItem struct {
Name string `yaml:"name"`
Context context `yaml:"context"`
Expand All @@ -38,16 +58,25 @@ type context struct {
Namespace string `yaml:"namespace,omitempty"`
}

type KubeConfig struct {
type KubeConfigBase struct {
APIVersion string `yaml:"apiVersion"`
Clusters []clusterItem `yaml:"clusters"`
Users []userItem `yaml:"users"`
Contexts []contextItem `yaml:"contexts,omitempty"`
CurrentContext string `yaml:"current-context,omitempty"`
Kind string `yaml:"kind,omitempty"`
Preferences map[string]interface{} `yaml:"preferences,omitempty"`
}

type KubeConfig struct {
KubeConfigBase `yaml:",inline"`
Users []userItem `yaml:"users"`
}

type KubeConfigAAD struct {
metacpp marked this conversation as resolved.
Show resolved Hide resolved
KubeConfigBase `yaml:",inline"`
Users []userItemAAD `yaml:"users"`
}

func ParseKubeConfig(config string) (*KubeConfig, error) {
if config == "" {
return nil, fmt.Errorf("Cannot parse empty config")
Expand All @@ -72,3 +101,25 @@ func ParseKubeConfig(config string) (*KubeConfig, error) {

return &kubeConfig, nil
}

func ParseKubeConfigAAD(config string) (*KubeConfigAAD, error) {
if config == "" {
return nil, fmt.Errorf("Cannot parse empty config")
}

var kubeConfig KubeConfigAAD
err := yaml.Unmarshal([]byte(config), &kubeConfig)
if err != nil {
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
return nil, fmt.Errorf("Failed to unmarshal YAML config with error %+v", err)
}
if len(kubeConfig.Clusters) <= 0 || len(kubeConfig.Users) <= 0 {
return nil, fmt.Errorf("Config %+v contains no valid clusters or users", kubeConfig)
}

cluster := kubeConfig.Clusters[0].Cluster
if cluster.Server == "" {
return nil, fmt.Errorf("Config has invalid or non existent server for cluster %+v", cluster)
}

return &kubeConfig, nil
}
104 changes: 55 additions & 49 deletions azurerm/helpers/kubernetes/kube_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ func TestParseKubeConfig(t *testing.T) {
{
"user_with_token.yml",
KubeConfig{
APIVersion: "v1",
Clusters: []clusterItem{
{
Name: "test-cluster",
Cluster: cluster{
Server: "https://testcluster.net:8080",
KubeConfigBase: KubeConfigBase{
APIVersion: "v1",
Clusters: []clusterItem{
{
Name: "test-cluster",
Cluster: cluster{
Server: "https://testcluster.net:8080",
},
},
},
Kind: "Config",
},
Users: []userItem{
{
Expand All @@ -34,22 +37,36 @@ func TestParseKubeConfig(t *testing.T) {
},
},
},
Kind: "Config",
},
isValidConfig,
},
{
"user_with_cert.yml",
KubeConfig{
APIVersion: "v1",
Clusters: []clusterItem{
{
Name: "test-cluster",
Cluster: cluster{
ClusterAuthorityData: "test-cluster-authority-data",
Server: "https://testcluster.org:443",
KubeConfigBase: KubeConfigBase{
APIVersion: "v1",
Clusters: []clusterItem{
{
Name: "test-cluster",
Cluster: cluster{
ClusterAuthorityData: "test-cluster-authority-data",
Server: "https://testcluster.org:443",
},
},
},
Contexts: []contextItem{
{
Name: "test-cluster",
Context: context{
Cluster: "test-cluster",
User: "test-user",
Namespace: "test-namespace",
},
},
},
CurrentContext: "test-cluster",
Kind: "Config",
Preferences: nil,
},
Users: []userItem{
{
Expand All @@ -60,34 +77,38 @@ func TestParseKubeConfig(t *testing.T) {
},
},
},
Contexts: []contextItem{
{
Name: "test-cluster",
Context: context{
Cluster: "test-cluster",
User: "test-user",
Namespace: "test-namespace",
},
},
},
CurrentContext: "test-cluster",
Kind: "Config",
Preferences: nil,
},
isValidConfig,
},
{
"user_with_cert_token.yml",
KubeConfig{
APIVersion: "v1",
Clusters: []clusterItem{
{
Name: "test-cluster",
Cluster: cluster{
ClusterAuthorityData: "test-cluster-authority-data",
Server: "https://testcluster.org:443",
KubeConfigBase: KubeConfigBase{
APIVersion: "v1",
Clusters: []clusterItem{
{
Name: "test-cluster",
Cluster: cluster{
ClusterAuthorityData: "test-cluster-authority-data",
Server: "https://testcluster.org:443",
},
},
},
Contexts: []contextItem{
{
Name: "test-cluster",
Context: context{
Cluster: "test-cluster",
User: "test-user",
Namespace: "test-namespace",
},
},
},
CurrentContext: "test-cluster",
Kind: "Config",
Preferences: map[string]interface{}{
"colors": true,
},
},
Users: []userItem{
{
Expand All @@ -99,21 +120,6 @@ func TestParseKubeConfig(t *testing.T) {
},
},
},
Contexts: []contextItem{
{
Name: "test-cluster",
Context: context{
Cluster: "test-cluster",
User: "test-user",
Namespace: "test-namespace",
},
},
},
CurrentContext: "test-cluster",
Kind: "Config",
Preferences: map[string]interface{}{
"colors": true,
},
},
isValidConfig,
},
Expand Down
Loading