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

Increase error verbosity for missing config vars #3

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module github.com/probr/probr-pack-aks
module github.com/probr/probr-pack

go 1.14

require (
github.com/cucumber/godog v0.11.0
github.com/hashicorp/go-hclog v0.15.0 // indirect
github.com/markbates/pkger v0.17.1
github.com/probr/probr-sdk v0.1.3
github.com/probr/probr-sdk v0.1.6
k8s.io/api v0.19.6
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,8 @@ github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6J
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/probr/probr-sdk v0.1.3 h1:wKbiECtenJXhwJOe+syNxHdbEQlnF1Ux7sQ3g3ZZ9SQ=
github.com/probr/probr-sdk v0.1.3/go.mod h1:Cp/BmkTE08Q0Kw+t3YcVn89ZjMuET2sSeEKsYEiYTFQ=
github.com/probr/probr-sdk v0.1.6 h1:NSv106oUtCOXCbj6tuDvEvsmiL8uHoxakWgVbC/C4Js=
github.com/probr/probr-sdk v0.1.6/go.mod h1:Cp/BmkTE08Q0Kw+t3YcVn89ZjMuET2sSeEKsYEiYTFQ=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM=
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs=
Expand Down
11 changes: 0 additions & 11 deletions internal/azure/aks/feature_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (

"github.com/probr/probr-pack-aks/internal/common"
"github.com/probr/probr-sdk/probeengine"
azureutil "github.com/probr/probr-sdk/providers/azure"
"github.com/probr/probr-sdk/providers/azure/connection"
)

type scenarioState struct {
Expand Down Expand Up @@ -49,15 +47,6 @@ func (probe probeStruct) Path() string {
func (probe probeStruct) ProbeInitialize(ctx *godog.TestSuiteContext) {

ctx.BeforeSuite(func() {

scenario.AZConnection = connection.NewAzureConnection(
context.Background(),
azureutil.SubscriptionID(),
azureutil.TenantID(),
azureutil.ClientID(),
azureutil.ClientSecret(),
)

})

ctx.AfterSuite(func() {
Expand Down
24 changes: 4 additions & 20 deletions internal/azure/kubernetes/encryption-at-rest/feature_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ import (

"github.com/probr/probr-pack-aks/internal/common"
"github.com/probr/probr-pack-aks/internal/config"
"github.com/probr/probr-pack-aks/internal/connection"
"github.com/probr/probr-pack-aks/internal/summary"

"github.com/probr/probr-sdk/probeengine"

azureutil "github.com/probr/probr-sdk/providers/azure"
azureconnection "github.com/probr/probr-sdk/providers/azure/connection"
k8sconnection "github.com/probr/probr-sdk/providers/kubernetes/connection"
)

type scenarioState struct {
Expand All @@ -33,8 +30,7 @@ type probeStruct struct {

// Probe ...
var Probe probeStruct
var scenario scenarioState // Local container of scenario state
var kConnection k8sconnection.Connection // Provides functionality to interact with Kubernetes
var scenario scenarioState // Local container of scenario state
var aksJSON []byte

func beforeScenario(s *scenarioState, probeName string, gs *godog.Scenario) {
Expand All @@ -61,18 +57,6 @@ func (probe probeStruct) Path() string {
func (probe probeStruct) ProbeInitialize(ctx *godog.TestSuiteContext) {

ctx.BeforeSuite(func() {

scenario.AZConnection = azureconnection.NewAzureConnection(
context.Background(),
azureutil.SubscriptionID(),
azureutil.TenantID(),
azureutil.ClientID(),
azureutil.ClientSecret(),
)

//TODO make this part of the scenario object
kConnection = k8sconnection.NewConnection(config.Vars.ServicePacks.Kubernetes.KubeConfigPath, config.Vars.ServicePacks.Kubernetes.KubeContext, config.Vars.ServicePacks.Kubernetes.ProbeNamespace)

})

ctx.AfterSuite(func() {
Expand Down Expand Up @@ -120,15 +104,15 @@ func teardown() {
//delete any resources you created here
//if config.Vars.ServicePacks.Kubernetes.KeepPods == "false" {
for _, podName := range scenario.pods {
err := kConnection.DeletePodIfExists(podName, scenario.namespace, Probe.Name())
err := connection.Kubernetes.DeletePodIfExists(podName, scenario.namespace, Probe.Name())
if err != nil {
log.Printf(fmt.Sprintf("[ERROR] Could not retrieve pod from namespace '%s' for deletion: %s", scenario.namespace, err))
}
}
//}

for _, pvcName := range scenario.pvcs {
err := kConnection.DeletePVCIfExists(pvcName, scenario.namespace, Probe.Name())
err := connection.Kubernetes.DeletePVCIfExists(pvcName, scenario.namespace, Probe.Name())
if err != nil {
log.Printf(fmt.Sprintf("[ERROR] Could not retrieve PVC from namespace '%s' for deletion: %s", scenario.namespace, err))
}
Expand Down
18 changes: 9 additions & 9 deletions internal/azure/kubernetes/encryption-at-rest/scenarios.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/probr/probr-pack-aks/internal/common"
"github.com/probr/probr-pack-aks/internal/config"
"github.com/probr/probr-pack-aks/internal/connection"
"github.com/probr/probr-sdk/providers/kubernetes/constructors"
"github.com/probr/probr-sdk/utils"
)
Expand All @@ -32,7 +33,7 @@ func (scenario *scenarioState) aKubernetesClusterIsDeployed() error {
config.Vars.ServicePacks.Kubernetes.KubeContext,
}

err = kConnection.ClusterIsDeployed() // Must be assigned to 'err' be audited
err = connection.Kubernetes.ClusterIsDeployed() // Must be assigned to 'err' be audited
return err
}

Expand All @@ -56,8 +57,8 @@ func (scenario *scenarioState) iCreateAPodWhichDynamicallyCreatesAnAzureDisk() e
constructors.AddPVCToPod(podObject, pvcObject)

stepTrace.WriteString("Create pod from spec; ")
createdPVCObject, pvcCreationErr := kConnection.CreatePVCFromObject(pvcObject, Probe.Name())
createdPodObject, podCreationErr := kConnection.CreatePodFromObject(podObject, Probe.Name()) // Pod name is saved to scenario state if successful
createdPVCObject, pvcCreationErr := connection.Kubernetes.CreatePVCFromObject(pvcObject, Probe.Name())
createdPodObject, podCreationErr := connection.Kubernetes.CreatePodFromObject(podObject, Probe.Name()) // Pod name is saved to scenario state if successful
if podCreationErr != nil {
return podCreationErr
}
Expand All @@ -72,7 +73,7 @@ func (scenario *scenarioState) iCreateAPodWhichDynamicallyCreatesAnAzureDisk() e
}

func (scenario *scenarioState) theDiskIsEncryptedUsingCustomerManagedKeys() error {
pvc, err := kConnection.GetPVCFromPVCName(scenario.pvcs[0], scenario.namespace)
pvc, err := connection.Kubernetes.GetPVCFromPVCName(scenario.pvcs[0], scenario.namespace)
if err != nil {
log.Printf("[DEBUG] Error getting PVC from PVC Name")
return err
Expand All @@ -86,12 +87,12 @@ func (scenario *scenarioState) theDiskIsEncryptedUsingCustomerManagedKeys() erro
log.Printf("[DEBUG] PVC Status.Phase: %s; Waiting...", pvc.Status.Phase)

time.Sleep(2 * time.Second)
pvc, _ = kConnection.GetPVCFromPVCName(scenario.pvcs[0], scenario.namespace)
pvc, _ = connection.Kubernetes.GetPVCFromPVCName(scenario.pvcs[0], scenario.namespace)
}

log.Printf("[DEBUG] PVC name is %s. PV name is %s.", scenario.pvcs[0], pvc.Spec.VolumeName)

pv, err := kConnection.GetPVFromPVName(pvc.Spec.VolumeName)
pv, err := connection.Kubernetes.GetPVFromPVName(pvc.Spec.VolumeName)
if err != nil {
log.Printf("[DEBUG] Error getting PV from PV Name")
log.Printf("[DEBUG] PVC trace: %v", pvc)
Expand All @@ -100,10 +101,10 @@ func (scenario *scenarioState) theDiskIsEncryptedUsingCustomerManagedKeys() erro

log.Printf("[DEBUG] Disk URI is %s", pv.Spec.AzureDisk.DataDiskURI)

rgName, diskName := scenario.AZConnection.ParseDiskDetails(pv.Spec.AzureDisk.DataDiskURI)
rgName, diskName := connection.Azure.ParseDiskDetails(pv.Spec.AzureDisk.DataDiskURI)
log.Printf("[DEBUG] Disk details are rgName: %s. diskName: %s", rgName, diskName)

azureDisk, err := scenario.AZConnection.GetDisk(rgName, diskName)
azureDisk, err := connection.Azure.GetDisk(rgName, diskName)
if err != nil {
log.Printf("Error getting disk client")
return err
Expand All @@ -120,7 +121,6 @@ func (scenario *scenarioState) theDiskIsEncryptedUsingCustomerManagedKeys() erro
}

func (scenario *scenarioState) anAzureKubernetesClusterWeCanReadTheConfigurationOf() (err error) {

aksJSON, err = common.AnAzureKubernetesClusterWeCanReadTheConfigurationOf(scenario.GetScenarioState())

return
Expand Down
57 changes: 19 additions & 38 deletions internal/azure/kubernetes/iam/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package iam

import (
"context"
"encoding/json"
"fmt"
"log"
Expand All @@ -12,15 +11,13 @@ import (

"github.com/probr/probr-pack-aks/internal/common"
"github.com/probr/probr-pack-aks/internal/config"
"github.com/probr/probr-pack-aks/internal/connection"
"github.com/probr/probr-pack-aks/internal/summary"

"github.com/probr/probr-sdk/audit"
"github.com/probr/probr-sdk/probeengine"
azureutil "github.com/probr/probr-sdk/providers/azure"
"github.com/probr/probr-sdk/providers/azure/aks"

azureconnection "github.com/probr/probr-sdk/providers/azure/connection"
"github.com/probr/probr-sdk/providers/kubernetes/connection"
k8sConnection "github.com/probr/probr-sdk/providers/kubernetes/connection"
"github.com/probr/probr-sdk/providers/kubernetes/constructors"
"github.com/probr/probr-sdk/providers/kubernetes/errors"
"github.com/probr/probr-sdk/utils"
Expand All @@ -41,10 +38,6 @@ type scenarioState struct {
// Probe meets the service pack interface for adding the logic from this file
var Probe probeStruct
var scenario scenarioState
var conn *connection.Conn
var azureK8S *aks.AKS

//var azConnection azureconnection.Azure // Provides functionality to interact with Azure

func (scenario *scenarioState) aKubernetesClusterIsDeployed() error {
// Standard auditing logic to ensures panics are also audited
Expand All @@ -65,7 +58,7 @@ func (scenario *scenarioState) aKubernetesClusterIsDeployed() error {
config.Vars.ServicePacks.Kubernetes.KubeContext,
}

err = conn.ClusterIsDeployed() // Must be assigned to 'err' be audited
err = connection.Kubernetes.ClusterIsDeployed() // Must be assigned to 'err' be audited
return err
}

Expand All @@ -92,7 +85,7 @@ func (scenario *scenarioState) aResourceTypeXCalledYExistsInNamespaceCalledZ(res
// TODO: This implementation is coupled to Azure. How should we deal with this when segregating service pack?

var foundInNamespace bool
var resource connection.APIResource
var resource k8sConnection.APIResource
var findErr error
// Validate input
switch resourceType {
Expand Down Expand Up @@ -123,7 +116,7 @@ func (scenario *scenarioState) aResourceTypeXCalledYExistsInNamespaceCalledZ(res
payload = struct {
CustomResourceType string
CustomResourceName string
Resource connection.APIResource
Resource k8sConnection.APIResource
}{
CustomResourceType: resourceType,
CustomResourceName: resourceName,
Expand Down Expand Up @@ -254,7 +247,7 @@ func (scenario *scenarioState) anAttemptToObtainAnAccessTokenFromThatPodShouldX(
cmd := "curl http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fmanagement.azure.com%2F -H Metadata:true -s"

stepTrace.WriteString(fmt.Sprintf("Attempt to run command in the pod: '%s'; ", cmd))
_, stdOut, _, cmdErr := conn.ExecCommand(cmd, scenario.namespace, podName)
_, stdOut, _, cmdErr := connection.Kubernetes.ExecCommand(cmd, scenario.namespace, podName)

// Validate that no internal error occurred during execution of curl command
if cmdErr != nil {
Expand Down Expand Up @@ -318,7 +311,7 @@ func (scenario *scenarioState) iCreateAnAzureIdentityBindingCalledInANondefaultN
Namespace string
AzureIdentityBindingName string
AzureIdentityName string
CreatedAzureIdentityBinding connection.APIResource
CreatedAzureIdentityBinding k8sConnection.APIResource
}{
Namespace: probrNameSpace,
AzureIdentityBindingName: aibName,
Expand All @@ -344,7 +337,7 @@ func (scenario *scenarioState) theClusterHasManagedIdentityComponentsDeployed()
stepTrace.WriteString(fmt.Sprintf(
"Get pods from '%s' namespace; ", identityPodsNamespace))
// look for the mic pods
podList, getErr := conn.GetPodsByNamespace(identityPodsNamespace)
podList, getErr := connection.Kubernetes.GetPodsByNamespace(identityPodsNamespace)

if getErr != nil {
err = utils.ReformatError("An error occurred when trying to retrieve pods %v", err)
Expand Down Expand Up @@ -424,7 +417,7 @@ func (scenario *scenarioState) theExecutionOfAXCommandInsideTheMICPodIsY(command
identityPodsNamespace := config.Vars.ServicePacks.AKS.ManagedID.IdentityNamespace
stepTrace.WriteString(fmt.Sprintf(
"Attempt to execute command '%s' in MIC pod '%s'; ", cmd, scenario.micPodName))
exitCode, stdOut, _, cmdErr := conn.ExecCommand(cmd, identityPodsNamespace, scenario.micPodName)
exitCode, stdOut, _, cmdErr := connection.Kubernetes.ExecCommand(cmd, identityPodsNamespace, scenario.micPodName)

// Validate that no internal error occurred during execution of curl command
if cmdErr != nil && exitCode == -1 {
Expand Down Expand Up @@ -482,7 +475,7 @@ func (scenario *scenarioState) checkClusterRBACForAdminRole() error {
//this is the role definition name for rolename "Azure Kubernetes Service Cluster Admin Role"
roleDefName := "0ab0b1a8-8aac-4efd-b8c2-3ee1fb270be8"

caRoleAssigned, err := scenario.AZConnection.ClusterHasRoleAssignment(config.Vars.ServicePacks.AKS.ResourceGroupName, config.Vars.ServicePacks.AKS.ClusterName, roleDefName)
caRoleAssigned, err := connection.Azure.ClusterHasRoleAssignment(config.Vars.ServicePacks.AKS.ResourceGroupName, config.Vars.ServicePacks.AKS.ClusterName, roleDefName)

if err != nil {
return err
Expand Down Expand Up @@ -510,7 +503,7 @@ func (scenario *scenarioState) checkCannotObtainClusterAdminCredentials() error
scenario.Audit.AuditScenarioStep(scenario.CurrentStep, stepTrace.String(), payload, err)
}()

_, credsErr := scenario.AZConnection.GetManagedClusterAdminCredentials(config.Vars.ServicePacks.AKS.ResourceGroupName, config.Vars.ServicePacks.AKS.ClusterName)
_, credsErr := connection.Azure.GetManagedClusterAdminCredentials(config.Vars.ServicePacks.AKS.ResourceGroupName, config.Vars.ServicePacks.AKS.ClusterName)

if credsErr != nil {
log.Printf("[DEBUG] Error trying to get cluster admin credentials: %v", err)
Expand Down Expand Up @@ -544,18 +537,6 @@ func (probe probeStruct) Path() string {
// test handler as part of the init() function.
func (probe probeStruct) ProbeInitialize(ctx *godog.TestSuiteContext) {
ctx.BeforeSuite(func() {
//conn = connection.Get()
conn = connection.NewConnection(config.Vars.ServicePacks.Kubernetes.KubeConfigPath, config.Vars.ServicePacks.Kubernetes.KubeContext, config.Vars.ServicePacks.Kubernetes.ProbeNamespace)
azureK8S = aks.NewAKS(conn)

scenario.AZConnection = azureconnection.NewAzureConnection(
context.Background(),
azureutil.SubscriptionID(),
azureutil.TenantID(),
azureutil.ClientID(),
azureutil.ClientSecret(),
)

//setup AzureIdentity stuff ..?? Or should this be a pre-test setup
})

Expand Down Expand Up @@ -611,7 +592,7 @@ func beforeScenario(s *scenarioState, probeName string, gs *godog.Scenario) {
func afterScenario(scenario scenarioState, probe probeStruct, gs *godog.Scenario, err error) {
if config.Vars.ServicePacks.Kubernetes.KeepPods == "false" {
for _, podName := range scenario.pods {
err = conn.DeletePodIfExists(podName, scenario.namespace, probe.Name())
err = connection.Kubernetes.DeletePodIfExists(podName, scenario.namespace, probe.Name())
if err != nil {
log.Printf(fmt.Sprintf("[ERROR] Could not retrieve pod from namespace '%s' for deletion: %s", scenario.namespace, err))
}
Expand All @@ -621,16 +602,16 @@ func afterScenario(scenario scenarioState, probe probeStruct, gs *godog.Scenario
}

func (scenario *scenarioState) createPodfromObject(podObject *apiv1.Pod) (createdPodObject *apiv1.Pod, err error) {
createdPodObject, err = conn.CreatePodFromObject(podObject, Probe.Name())
createdPodObject, err = connection.Kubernetes.CreatePodFromObject(podObject, Probe.Name())
if err == nil {
scenario.pods = append(scenario.pods, createdPodObject.ObjectMeta.Name)
}
return
}

func azureIdentityExistsInNamespace(azureIdentityName, namespace string) (exists bool, resource connection.APIResource, err error) {
func azureIdentityExistsInNamespace(azureIdentityName, namespace string) (exists bool, resource k8sConnection.APIResource, err error) {

resource, getError := azureK8S.GetIdentityByNameAndNamespace(azureIdentityName, namespace)
resource, getError := connection.AKS.GetIdentityByNameAndNamespace(azureIdentityName, namespace)
if getError != nil {
if errors.IsStatusCode(404, getError) {
exists = false
Expand All @@ -644,9 +625,9 @@ func azureIdentityExistsInNamespace(azureIdentityName, namespace string) (exists
return
}

func azureIdentityBindingExistsInNamespace(azureIdentityBindingName, namespace string) (exists bool, resource connection.APIResource, err error) {
func azureIdentityBindingExistsInNamespace(azureIdentityBindingName, namespace string) (exists bool, resource k8sConnection.APIResource, err error) {

resource, getError := azureK8S.GetIdentityBindingByNameAndNamespace(azureIdentityBindingName, namespace)
resource, getError := connection.AKS.GetIdentityBindingByNameAndNamespace(azureIdentityBindingName, namespace)
if getError != nil {
if errors.IsStatusCode(404, getError) {
exists = false
Expand All @@ -661,9 +642,9 @@ func azureIdentityBindingExistsInNamespace(azureIdentityBindingName, namespace s
}

// azureCreateAIB creates an AzureIdentityBinding in the cluster
func azureCreateAIB(namespace, aibName, aiName string) (aibResource connection.APIResource, err error) {
func azureCreateAIB(namespace, aibName, aiName string) (aibResource k8sConnection.APIResource, err error) {

resource, createErr := azureK8S.CreateAIB(namespace, aibName, aiName)
resource, createErr := connection.AKS.CreateAIB(namespace, aibName, aiName)
if errors.IsStatusCode(409, createErr) { // Already Exists
// TODO: Delete and recreate ?
createErr = nil
Expand Down
Loading