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

Refactoring to fail on vsphere perms validations #6188

Merged
merged 2 commits into from
Jul 17, 2023
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
31 changes: 15 additions & 16 deletions pkg/providers/vsphere/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"gopkg.in/yaml.v2"

"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/config"
Expand Down Expand Up @@ -170,7 +169,7 @@ func (v *Validator) ValidateClusterMachineConfigs(ctx context.Context, vsphereCl
logger.MarkPass("Control plane and Workload templates validated")

for _, mc := range vsphereClusterSpec.VSphereMachineConfigs {
if mc.OSFamily() == v1alpha1.Bottlerocket {
if mc.OSFamily() == anywherev1.Bottlerocket {
if err := v.validateBRHardDiskSize(ctx, vsphereClusterSpec, mc); err != nil {
return fmt.Errorf("failed validating BR Hard Disk size: %v", err)
}
Expand Down Expand Up @@ -476,15 +475,15 @@ func (v *Validator) validatePrivs(ctx context.Context, privObjs []PrivAssociatio
var err error
missingPrivs := []missingPriv{}
passed := false
username := vsc.Username()

for _, obj := range privObjs {
path := obj.path
privsContent := obj.privsContent
t := obj.objectType
username := vsc.Username()
privs, err = v.getMissingPrivs(ctx, vsc, path, t, privsContent, username)
if err != nil {
return passed, err
return passed, fmt.Errorf("failed to get missing privileges: %v", err)
} else if len(privs) > 0 {
mp := missingPriv{
Username: username,
Expand All @@ -493,23 +492,23 @@ func (v *Validator) validatePrivs(ctx context.Context, privObjs []PrivAssociatio
Permissions: privs,
}
missingPrivs = append(missingPrivs, mp)
content, err := yaml.Marshal(mp)
if err == nil {
s := fmt.Sprintf(" Warning: User %s missing %d vSphere permissions on %s, cluster creation may fail.\nRe-run create cluster with --verbosity=3 to see specific missing permissions.", username, len(privs), path)
logger.MarkWarning(s)
s = fmt.Sprintf("Missing Permissions:\n%s", string(content))
logger.V(3).Info(s)
} else {
s := fmt.Sprintf(" Warning: failed to list missing privs: %v", err)
logger.MarkWarning(s)
}
}
}

if len(missingPrivs) == 0 {
passed = true
if len(missingPrivs) != 0 {
content, err := yaml.Marshal(missingPrivs)
if err != nil {
return passed, fmt.Errorf("failed to marshal missing permissions: %v", err)
}

errMsg := fmt.Sprintf("user %s missing vSphere permissions", username)
logger.V(3).Info(errMsg, "Permissions", string(content))

return passed, fmt.Errorf("user %s missing vSphere permissions", username)
}

passed = true

return passed, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/providers/vsphere/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestValidatorValidatePrivsMissing(t *testing.T) {
passed, err := v.validatePrivs(ctx, objects, vsc)

g.Expect(passed).To(BeEquivalentTo(false))
g.Expect(err).To(BeNil())
g.Expect(err).NotTo(BeNil())
}

func TestValidatorValidatePrivsBadJson(t *testing.T) {
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestValidatorValidateVsphereCPUserPrivsError(t *testing.T) {
}

var privs []string
err := json.Unmarshal([]byte(config.VSphereUserPrivsFile), &privs)
err := json.Unmarshal([]byte(config.VSphereAdminPrivsFile), &privs)
if err != nil {
t.Fatalf("failed to validate privs: %v", err)
}
Expand Down