Skip to content

Commit e3170be

Browse files
authored
⭐️ output service account credential (#79)
* ⭐️ output service account credential * 🧹 update go mod
1 parent 7550aed commit e3170be

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

docs/resources/service_account.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ provider "mondoo" {
2929
}
3030
3131
resource "mondoo_space" "my_space" {
32-
name = "My Space Name"
32+
name = "My Terraform Space"
3333
org_id = var.mondoo_org
3434
}
3535
3636
resource "mondoo_service_account" "service_account" {
37-
name = "Service Account Terraform New"
37+
name = "Service Account Terraform"
3838
description = "Service Account for Terraform"
3939
roles = [
4040
"//iam.api.mondoo.app/roles/viewer",
@@ -45,6 +45,18 @@ resource "mondoo_service_account" "service_account" {
4545
mondoo_space.my_space
4646
]
4747
}
48+
49+
output "service_account_json" {
50+
description = "Service Account as JSON"
51+
value = base64decode(mondoo_service_account.service_account.credential)
52+
sensitive = true
53+
}
54+
55+
output "service_account_base64" {
56+
description = "Service Account as Base64"
57+
value = mondoo_service_account.service_account.credential
58+
sensitive = true
59+
}
4860
```
4961

5062
<!-- schema generated by tfplugindocs -->
@@ -60,4 +72,5 @@ resource "mondoo_service_account" "service_account" {
6072

6173
### Read-Only
6274

75+
- `credential` (String, Sensitive) The service account credential in JSON format, base64 encoded. This is the same content when creating service account credentials through the web console.
6376
- `mrn` (String) The Mondoo Resource Name (MRN) of the created service account.

examples/resources/mondoo_service_account/resource.tf

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ provider "mondoo" {
1414
}
1515

1616
resource "mondoo_space" "my_space" {
17-
name = "My Space Name"
17+
name = "My Terraform Space"
1818
org_id = var.mondoo_org
1919
}
2020

2121
resource "mondoo_service_account" "service_account" {
22-
name = "Service Account Terraform New"
22+
name = "Service Account Terraform"
2323
description = "Service Account for Terraform"
2424
roles = [
2525
"//iam.api.mondoo.app/roles/viewer",
@@ -30,3 +30,16 @@ resource "mondoo_service_account" "service_account" {
3030
mondoo_space.my_space
3131
]
3232
}
33+
34+
output "service_account_json" {
35+
description = "Service Account as JSON"
36+
value = base64decode(mondoo_service_account.service_account.credential)
37+
sensitive = true
38+
}
39+
40+
output "service_account_base64" {
41+
description = "Service Account as Base64"
42+
value = mondoo_service_account.service_account.credential
43+
sensitive = true
44+
}
45+

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/hashicorp/terraform-plugin-go v0.22.2
1111
github.com/hashicorp/terraform-plugin-log v0.9.0
1212
github.com/hashicorp/terraform-plugin-testing v1.7.0
13+
github.com/stretchr/testify v1.9.0
1314
go.mondoo.com/mondoo-go v0.0.0-20240303102235-bc102d6ef0cb
1415
)
1516

@@ -32,6 +33,7 @@ require (
3233
github.com/cli/safeexec v1.0.0 // indirect
3334
github.com/cli/shurcooL-graphql v0.0.2 // indirect
3435
github.com/cloudflare/circl v1.3.7 // indirect
36+
github.com/davecgh/go-spew v1.1.1 // indirect
3537
github.com/fatih/color v1.16.0 // indirect
3638
github.com/fsnotify/fsnotify v1.5.4 // indirect
3739
github.com/go-jose/go-jose/v3 v3.0.2 // indirect
@@ -88,6 +90,7 @@ require (
8890
github.com/muesli/termenv v0.12.0 // indirect
8991
github.com/oklog/run v1.0.0 // indirect
9092
github.com/oklog/ulid v1.3.1 // indirect
93+
github.com/pmezard/go-difflib v1.0.0 // indirect
9194
github.com/posener/complete v1.2.3 // indirect
9295
github.com/rivo/uniseg v0.2.0 // indirect
9396
github.com/samber/lo v1.37.0 // indirect

internal/provider/service_account_resource.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package provider
55

66
import (
77
"context"
8+
"encoding/base64"
9+
"encoding/json"
810
"fmt"
911
"github.com/hashicorp/terraform-plugin-framework/diag"
1012
"github.com/hashicorp/terraform-plugin-framework/path"
@@ -26,6 +28,17 @@ var _ resource.Resource = &ServiceAccountResource{}
2628

2729
var defaultRoles = []string{"//iam.api.mondoo.app/roles/viewer"}
2830

31+
// serviceAccountCredential is a temporary object until the API returns the credential as a string.
32+
type serviceAccountCredential struct {
33+
Mrn string `json:"mrn,omitempty"`
34+
PrivateKey string `json:"private_key,omitempty"`
35+
Certificate string `json:"certificate,omitempty"`
36+
ApiEndpoint string `json:"api_endpoint,omitempty"`
37+
ScopeMrn string `json:"scope_mrn,omitempty"`
38+
// ParentMrn is deprecated and should not be used, use ScopeMrn instead
39+
ParentMrn string `json:"parent_mrn,omitempty"`
40+
}
41+
2942
func NewServiceAccountResource() resource.Resource {
3043
return &ServiceAccountResource{}
3144
}
@@ -46,6 +59,9 @@ type ServiceAccountResourceModel struct {
4659
Name types.String `tfsdk:"name"`
4760
Description types.String `tfsdk:"description"`
4861
Roles types.List `tfsdk:"roles"`
62+
63+
// base 64 encoded service account credential
64+
Credential types.String `tfsdk:"credential"`
4965
}
5066

5167
func (r *ServiceAccountResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
@@ -99,6 +115,14 @@ func (r *ServiceAccountResource) Schema(ctx context.Context, req resource.Schema
99115
listplanmodifier.UseStateForUnknown(),
100116
},
101117
},
118+
"credential": schema.StringAttribute{
119+
Computed: true,
120+
MarkdownDescription: "The service account credential in JSON format, base64 encoded. This is the same content when creating service account credentials through the web console.",
121+
PlanModifiers: []planmodifier.String{
122+
stringplanmodifier.UseStateForUnknown(),
123+
},
124+
Sensitive: true,
125+
},
102126
},
103127
}
104128
}
@@ -206,7 +230,25 @@ func (r *ServiceAccountResource) Create(ctx context.Context, req resource.Create
206230
// Save space mrn into the Terraform state.
207231
data.Name = types.StringValue(name)
208232
data.Mrn = types.StringValue(string(createMutation.CreateServiceAccount.Mrn))
209-
// TODO: add certificate and private key
233+
234+
// NOTE: this is temporary, we want to change the API to return the credential as a string
235+
serviceAccount := serviceAccountCredential{
236+
Mrn: string(createMutation.CreateServiceAccount.Mrn),
237+
PrivateKey: string(createMutation.CreateServiceAccount.PrivateKey),
238+
Certificate: string(createMutation.CreateServiceAccount.Certificate),
239+
ApiEndpoint: string(createMutation.CreateServiceAccount.ApiEndpoint),
240+
ScopeMrn: string(createMutation.CreateServiceAccount.ScopeMrn),
241+
ParentMrn: string(createMutation.CreateServiceAccount.ScopeMrn),
242+
}
243+
244+
jsonData, err := json.Marshal(serviceAccount)
245+
if err != nil {
246+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create service account, got error: %s", err))
247+
return
248+
}
249+
250+
// set base 64 encoded credential
251+
data.Credential = types.StringValue(base64.StdEncoding.EncodeToString(jsonData))
210252

211253
// Write logs using the tflog package
212254
tflog.Trace(ctx, "created a service account resource")

0 commit comments

Comments
 (0)