Skip to content

Commit

Permalink
add key type param
Browse files Browse the repository at this point in the history
  • Loading branch information
rubencosta committed Nov 15, 2024
1 parent 3022702 commit b76429c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/resources/nkey.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ An nkey is an ed25519 key pair formatted for use with NATS.
<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `type` (String) The type of nkey to generate. Must be one of user|account|server|cluster|operator|curve

### Read-Only

- `private_key` (String, Sensitive) Private key of the nkey to be given to the client for authentication
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.21
require (
github.com/hashicorp/terraform-plugin-docs v0.19.4
github.com/hashicorp/terraform-plugin-framework v1.11.0
github.com/hashicorp/terraform-plugin-go v0.23.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/nats-io/nkeys v0.4.7
)
Expand Down Expand Up @@ -38,6 +37,7 @@ require (
github.com/hashicorp/hc-install v0.8.0 // indirect
github.com/hashicorp/terraform-exec v0.21.0 // indirect
github.com/hashicorp/terraform-json v0.22.1 // indirect
github.com/hashicorp/terraform-plugin-go v0.23.0 // indirect
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
Expand Down
28 changes: 26 additions & 2 deletions internal/provider/nkey_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package provider

import (
"context"
"strings"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"

Expand All @@ -29,6 +31,7 @@ type Nkey struct {

// NkeyModel describes the resource data model.
type NkeyModel struct {
KeyType types.String `tfsdk:"type"`
Public_key types.String `tfsdk:"public_key"`
Private_key types.String `tfsdk:"private_key"`
}
Expand All @@ -43,6 +46,12 @@ func (r *Nkey) Schema(ctx context.Context, req resource.SchemaRequest, resp *res
MarkdownDescription: "An nkey is an ed25519 key pair formatted for use with NATS.",

Attributes: map[string]schema.Attribute{
"type": schema.StringAttribute{
Optional: true,
Computed: true,
Default: stringdefault.StaticString("account"),
Description: "The type of nkey to generate. Must be one of user|account|server|cluster|operator|curve",
},
"public_key": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Public key of the nkey to be given in config to the nats server",
Expand Down Expand Up @@ -70,12 +79,27 @@ func (r *Nkey) Create(ctx context.Context, req resource.CreateRequest, resp *res
return
}

keys, err := nkeys.CreateAccount()
var keys nkeys.KeyPair
var err error
switch strings.ToLower(data.KeyType.ValueString()) {
case "user":
keys, err = nkeys.CreateUser()
case "account":
keys, err = nkeys.CreateAccount()
case "server":
keys, err = nkeys.CreateServer()
case "cluster":
keys, err = nkeys.CreateCluster()
case "operator":
keys, err = nkeys.CreateOperator()
case "curve":
keys, err = nkeys.CreateCurveKeys()
}

if err != nil {
resp.Diagnostics.AddError("generating nkey", err.Error())
return
}

pubKey, err := keys.PublicKey()
if err != nil {
resp.Diagnostics.AddError("accessing public nkey", err.Error())
Expand Down

0 comments on commit b76429c

Please sign in to comment.