-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix detection of default service account usage on GCP compute in…
…stance (#853) * fix: Fix detection of default service account usage on GCP compute instance Resolves #821 Signed-off-by: Liam Galvin <[email protected]>
- Loading branch information
Showing
6 changed files
with
66 additions
and
30 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
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 |
---|---|---|
|
@@ -21,9 +21,10 @@ func adaptInstances(modules terraform.Modules) (instances []compute.Instance) { | |
VTPMEnabled: defsecTypes.BoolDefault(false, instanceBlock.GetMetadata()), | ||
}, | ||
ServiceAccount: compute.ServiceAccount{ | ||
Metadata: instanceBlock.GetMetadata(), | ||
Email: defsecTypes.StringDefault("", instanceBlock.GetMetadata()), | ||
Scopes: nil, | ||
Metadata: instanceBlock.GetMetadata(), | ||
Email: defsecTypes.StringDefault("", instanceBlock.GetMetadata()), | ||
IsDefault: defsecTypes.BoolDefault(false, instanceBlock.GetMetadata()), | ||
Scopes: nil, | ||
}, | ||
CanIPForward: instanceBlock.GetAttribute("can_ip_forward").AsBoolValueOrDefault(false, instanceBlock), | ||
OSLoginEnabled: defsecTypes.BoolDefault(true, instanceBlock.GetMetadata()), | ||
|
@@ -57,11 +58,6 @@ func adaptInstances(modules terraform.Modules) (instances []compute.Instance) { | |
instance.ShieldedVM.SecureBootEnabled = shieldedBlock.GetAttribute("enable_secure_boot").AsBoolValueOrDefault(false, shieldedBlock) | ||
} | ||
|
||
if serviceAccountBlock := instanceBlock.GetBlock("service_account"); serviceAccountBlock.IsNotNil() { | ||
instance.ServiceAccount.Metadata = serviceAccountBlock.GetMetadata() | ||
instance.ServiceAccount.Email = serviceAccountBlock.GetAttribute("email").AsStringValueOrDefault("", serviceAccountBlock) | ||
} | ||
|
||
// metadata | ||
if metadataAttr := instanceBlock.GetAttribute("metadata"); metadataAttr.IsNotNil() { | ||
if val := metadataAttr.MapValue("enable-oslogin"); val.Type() == cty.Bool { | ||
|
@@ -101,17 +97,22 @@ func adaptInstances(modules terraform.Modules) (instances []compute.Instance) { | |
instance.AttachedDisks = append(instance.AttachedDisks, disk) | ||
} | ||
|
||
if instanceBlock.GetBlock("service_account").IsNotNil() { | ||
emailAttr := instanceBlock.GetBlock("service_account").GetAttribute("email") | ||
instance.ServiceAccount.Email = emailAttr.AsStringValueOrDefault("", instanceBlock) | ||
if serviceAccountBlock := instanceBlock.GetBlock("service_account"); serviceAccountBlock.IsNotNil() { | ||
emailAttr := serviceAccountBlock.GetAttribute("email") | ||
instance.ServiceAccount.Email = emailAttr.AsStringValueOrDefault("", serviceAccountBlock) | ||
|
||
if instance.ServiceAccount.Email.IsEmpty() || instance.ServiceAccount.Email.EndsWith("[email protected]") { | ||
instance.ServiceAccount.IsDefault = defsecTypes.Bool(true, serviceAccountBlock.GetMetadata()) | ||
} | ||
|
||
if emailAttr.IsResourceBlockReference("google_service_account") { | ||
if accBlock, err := modules.GetReferencedBlock(emailAttr, instanceBlock); err == nil { | ||
instance.ServiceAccount.Email = defsecTypes.String(accBlock.FullName(), emailAttr.GetMetadata()) | ||
instance.ServiceAccount.IsDefault = defsecTypes.Bool(false, serviceAccountBlock.GetMetadata()) | ||
instance.ServiceAccount.Email = accBlock.GetAttribute("email").AsStringValueOrDefault("", accBlock) | ||
} | ||
} | ||
|
||
if scopesAttr := instanceBlock.GetBlock("service_account").GetAttribute("scopes"); scopesAttr.IsNotNil() { | ||
if scopesAttr := serviceAccountBlock.GetAttribute("scopes"); scopesAttr.IsNotNil() { | ||
instance.ServiceAccount.Scopes = append(instance.ServiceAccount.Scopes, scopesAttr.AsStringValues()...) | ||
} | ||
} | ||
|
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
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 |
---|---|---|
|
@@ -32,7 +32,7 @@ var CheckNoDefaultServiceAccount = rules.Register( | |
if instance.IsUnmanaged() { | ||
continue | ||
} | ||
if instance.ServiceAccount.Email.IsEmpty() || instance.ServiceAccount.Email.EndsWith("[email protected]") { | ||
if instance.ServiceAccount.IsDefault.IsTrue() { | ||
results.Add( | ||
"Instance uses the default service account.", | ||
instance.ServiceAccount.Email, | ||
|
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 |
---|---|---|
|
@@ -20,14 +20,15 @@ func TestCheckNoDefaultServiceAccount(t *testing.T) { | |
expected bool | ||
}{ | ||
{ | ||
name: "Instance service account missing email", | ||
name: "Instance service account not specified", | ||
input: compute.Compute{ | ||
Instances: []compute.Instance{ | ||
{ | ||
Metadata: defsecTypes.NewTestMetadata(), | ||
ServiceAccount: compute.ServiceAccount{ | ||
Metadata: defsecTypes.NewTestMetadata(), | ||
Email: defsecTypes.String("", defsecTypes.NewTestMetadata()), | ||
Metadata: defsecTypes.NewTestMetadata(), | ||
Email: defsecTypes.String("", defsecTypes.NewTestMetadata()), | ||
IsDefault: defsecTypes.Bool(true, defsecTypes.NewTestMetadata()), | ||
}, | ||
}, | ||
}, | ||
|
@@ -41,8 +42,9 @@ func TestCheckNoDefaultServiceAccount(t *testing.T) { | |
{ | ||
Metadata: defsecTypes.NewTestMetadata(), | ||
ServiceAccount: compute.ServiceAccount{ | ||
Metadata: defsecTypes.NewTestMetadata(), | ||
Email: defsecTypes.String("[email protected]", defsecTypes.NewTestMetadata()), | ||
Metadata: defsecTypes.NewTestMetadata(), | ||
Email: defsecTypes.String("[email protected]", defsecTypes.NewTestMetadata()), | ||
IsDefault: defsecTypes.Bool(true, defsecTypes.NewTestMetadata()), | ||
}, | ||
}, | ||
}, | ||
|
@@ -56,8 +58,9 @@ func TestCheckNoDefaultServiceAccount(t *testing.T) { | |
{ | ||
Metadata: defsecTypes.NewTestMetadata(), | ||
ServiceAccount: compute.ServiceAccount{ | ||
Metadata: defsecTypes.NewTestMetadata(), | ||
Email: defsecTypes.String("[email protected]", defsecTypes.NewTestMetadata()), | ||
Metadata: defsecTypes.NewTestMetadata(), | ||
Email: defsecTypes.String("[email protected]", defsecTypes.NewTestMetadata()), | ||
IsDefault: defsecTypes.Bool(false, defsecTypes.NewTestMetadata()), | ||
}, | ||
}, | ||
}, | ||
|
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