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

Add support for ServiceAccount records #12

Merged
merged 4 commits into from
Jan 18, 2024
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
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,30 @@ specified type. Any flag set will be inserted instead of a random value.
All remaining fields will be filled with a suitably random value.

SUBCOMMANDS
user generate a user identity JSON record
internal generate an internal identity JSON record
system generate a system identity JSON record
associate generate an associate identity JSON record
user generate a user identity JSON record
internal generate an internal identity JSON record
system generate a system identity JSON record
associate generate an associate identity JSON record
service-account generate a service account identity JSON record

FLAGS
-account-number ... set the identity.account_number field (string)
-auth-type ... set the identity.authtype field (string)
-employe-account-number ... set the identity.employee_account_number field (string)
-org-id ... set the identity.org_id field (string)
-type ... set the identity.type field (string)
-account-number value set the identity.account_number field (string)
-auth-type value set the identity.authtype field (string)
-employe-account-number value set the identity.employee_account_number field (string)
-org-id value set the identity.org_id field (string)
-type value set the identity.type field (string)
```

### Examples

```
$ xrhidgen user -email [email protected]
{"identity":{"account_number":"16349","auth_type":"cert","employee_account_number":"06900","internal":{"org_id":"51818"},"org_id":"51818","type":"User","user":{"email":"[email protected]","first_name":"Quinn","is_active":true,"is_internal":true,"is_org_admin":true,"last_name":"Runolfsdottir","locale":"se","user_id":"taps","username":"dunstable"}}}
{"identity":{"org_id":"45841","internal":{"org_id":"45841"},"user":{"username":"backbencher","email":"[email protected]","first_name":"Cira","last_name":"Roob","is_active":false,"is_org_admin":true,"is_internal":false,"locale":"li","user_id":"nicole"},"system":{},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"","issuer_dn":""},"service_account":{"client_id":"","username":""},"type":"User","auth_type":"cert-auth"},"entitlements":null}
```

```
$ xrhidgen system | base64 -w0
eyJpZGVudGl0eSI6eyJhY2NvdW50X251bWJlciI6IjQ0NDY4OCIsImVtcGxveWVlX2FjY291bnRfbnVtYmVyIjoiIiwib3JnX2lkIjoiODEzNTIiLCJzeXN0ZW0iOnsiY2VydF90eXBlIjoiIiwiY2x1c3Rlcl9pZCI6ImNjWWJhTllCIiwiY24iOiJhYzRlM2RmYy1kOGU3LTQwODUtYjg3YS0zMTcyZjU1M2I3M2UifSwidHlwZSI6IiJ9fQo=
eyJpZGVudGl0eSI6eyJvcmdfaWQiOiIyMTcwMyIsImludGVybmFsIjp7Im9yZ19pZCI6IjIxNzAzIn0sInVzZXIiOnsidXNlcm5hbWUiOiIiLCJlbWFpbCI6IiIsImZpcnN0X25hbWUiOiIiLCJsYXN0X25hbWUiOiIiLCJpc19hY3RpdmUiOmZhbHNlLCJpc19vcmdfYWRtaW4iOmZhbHNlLCJpc19pbnRlcm5hbCI6ZmFsc2UsImxvY2FsZSI6IiIsInVzZXJfaWQiOiIifSwic3lzdGVtIjp7ImNuIjoiRyIsImNlcnRfdHlwZSI6ImNvbnN1bWVyIiwiY2x1c3Rlcl9pZCI6IkhKbyJ9LCJhc3NvY2lhdGUiOnsiUm9sZSI6bnVsbCwiZW1haWwiOiIiLCJnaXZlbk5hbWUiOiIiLCJyaGF0VVVJRCI6IiIsInN1cm5hbWUiOiIifSwieDUwOSI6eyJzdWJqZWN0X2RuIjoiIiwiaXNzdWVyX2RuIjoiIn0sInNlcnZpY2VfYWNjb3VudCI6eyJjbGllbnRfaWQiOiIiLCJ1c2VybmFtZSI6IiJ9LCJ0eXBlIjoiU3lzdGVtIiwiYXV0aF90eXBlIjoiYmFzaWMtYXV0aCJ9LCJlbnRpdGxlbWVudHMiOm51bGx9Cg==
```

```
Expand All @@ -60,7 +61,7 @@ used to initialize the generator to a deterministic state.

```
$ SEED=100 xrhidgen user
{"identity":{"auth_type":"basic","employee_account_number":"02299","internal":{"org_id":"41123"},"org_id":"41123","type":"User","user":{"email":"[email protected]","first_name":"Cameron","is_active":false,"is_internal":false,"is_org_admin":false,"last_name":"Stehr","locale":"fi","user_id":"meredeth","username":"skeptic"}}}
{"identity":{"employee_account_number":"02299","org_id":"41123","internal":{"org_id":"41123"},"user":{"username":"skeptic","email":"[email protected]","first_name":"Cameron","last_name":"Stehr","is_active":false,"is_org_admin":false,"is_internal":false,"locale":"fi","user_id":"meredeth"},"system":{},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"","issuer_dn":""},"service_account":{"client_id":"","username":""},"type":"User","auth_type":"basic-auth"},"entitlements":null}
```

## Go package
Expand Down
2 changes: 1 addition & 1 deletion associate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package xrhidgen

import (
"github.com/pioz/faker"
"github.com/redhatinsights/platform-go-middlewares/identity"
"github.com/redhatinsights/platform-go-middlewares/v2/identity"
)

// Associate holds values to be used as input when generating an associate
Expand Down
2 changes: 1 addition & 1 deletion associate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/pioz/faker"
"github.com/redhatinsights/platform-go-middlewares/identity"
"github.com/redhatinsights/platform-go-middlewares/v2/identity"
"go.openly.dev/pointy"
)

Expand Down
42 changes: 22 additions & 20 deletions cmd/xrhidgen/main.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
// xrhidgen generates X-Rh-Identity records.
//
// Usage:
// xrhidgen [flags] <subcommand>

// xrhidgen can be used to generate JSON records suitable for passing in to
// the X-Rh-Identity header. Each subcommand will generate a record of the
// specified type. Any flag set will be inserted instead of a random value.
// All remaining fields will be filled with a suitably random value.

// SUBCOMMANDS
// user generate a user identity JSON record
// internal generate an internal identity JSON record
// system generate a system identity JSON record
// associate generate an associate identity JSON record

// FLAGS
// -account-number ... set the identity.account_number field (string)
// -auth-type ... set the identity.authtype field (string)
// -employe-account-number ... set the identity.employee_account_number field (string)
// -org-id ... set the identity.org_id field (string)
// -type ... set the identity.type field (string)
// USAGE
// xrhidgen [flags] <subcommand>
//
// xrhidgen can be used to generate JSON records suitable for passing in to
// the X-Rh-Identity header. Each subcommand will generate a record of the
// specified type. Any flag set will be inserted instead of a random value.
// All remaining fields will be filled with a suitably random value.
//
// SUBCOMMANDS
// user generate a user identity JSON record
// internal generate an internal identity JSON record
// system generate a system identity JSON record
// associate generate an associate identity JSON record
// service-account generate a service account identity JSON record
//
// FLAGS
// -account-number value set the identity.account_number field (string)
// -auth-type value set the identity.authtype field (string)
// -employe-account-number value set the identity.employee_account_number field (string)
// -org-id value set the identity.org_id field (string)
// -type value set the identity.type field (string)
package main

import (
Expand Down Expand Up @@ -61,6 +62,7 @@ func main() {
internalCommand,
systemCommand,
associateCommand,
serviceAccountCommand,
},
Exec: func(context.Context, []string) error {
return flag.ErrHelp
Expand Down
53 changes: 53 additions & 0 deletions cmd/xrhidgen/service_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"context"
"encoding/json"
"flag"
"fmt"

"github.com/peterbourgon/ff/v3/ffcli"
"github.com/subpop/xrhidgen"
)

var serviceAccountFlags struct {
clientID StringFlag
username StringFlag
}

func NewServiceAccountFlagSet(name string, errorHandling flag.ErrorHandling) *flag.FlagSet {
fs := flag.NewFlagSet(name, errorHandling)

fs.Var(&serviceAccountFlags.clientID, "client-id", "set the identity.service_account.client_id field (string)")
fs.Var(&serviceAccountFlags.username, "username", "set the identity.service_account.username field (string)")

return fs
}

var serviceAccountCommand = &ffcli.Command{
Name: "service-account",
ShortUsage: "service-account [flags]",
ShortHelp: "generate a service account identity JSON record",
LongHelp: WordWrap("Generate a service account identity record, populating fields with values provided by the matching flag. Any omitted flags will have their corresponding fields populated with a suitable random value.", 72),
FlagSet: NewServiceAccountFlagSet("service-account", flag.ExitOnError),
Exec: func(ctx context.Context, args []string) error {
serviceAccount := xrhidgen.ServiceAccount{
ClientID: serviceAccountFlags.clientID.Value,
Username: serviceAccountFlags.username.Value,
}

id, err := xrhidgen.NewServiceAccountIdentity(mainIdentity(), serviceAccount)
if err != nil {
return err
}

data, err := json.Marshal(id)
if err != nil {
return fmt.Errorf("cannot marshal data: %w", err)
}

fmt.Println(string(data))

return nil
},
}
24 changes: 19 additions & 5 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func ExampleAssociate() {
panic(err)
}
fmt.Println(string(data))
//Output: {"identity":{"employee_account_number":"02299","org_id":"41123","internal":{"org_id":""},"user":{"username":"","email":"","first_name":"","last_name":"","is_active":false,"is_org_admin":false,"is_internal":false,"locale":"","user_id":""},"system":{},"associate":{"Role":null,"email":"[email protected]","givenName":"Cameron","rhatUUID":"00e3c758-1d7d-4ecd-98a2-997157e2d05c","surname":"Swift"},"x509":{"subject_dn":"","issuer_dn":""},"type":"Associate","auth_type":"basic-auth"}}
//Output: {"identity":{"employee_account_number":"02299","org_id":"41123","internal":{"org_id":""},"user":{"username":"","email":"","first_name":"","last_name":"","is_active":false,"is_org_admin":false,"is_internal":false,"locale":"","user_id":""},"system":{},"associate":{"Role":null,"email":"[email protected]","givenName":"Cameron","rhatUUID":"00e3c758-1d7d-4ecd-98a2-997157e2d05c","surname":"Swift"},"x509":{"subject_dn":"","issuer_dn":""},"service_account":{"client_id":"","username":""},"type":"Associate","auth_type":"basic-auth"},"entitlements":null}
}

func ExampleInternal() {
Expand All @@ -32,7 +32,7 @@ func ExampleInternal() {
panic(err)
}
fmt.Println(string(data))
//Output: {"identity":{"org_id":"03797","internal":{"org_id":"08321","auth_time":-2978345600000000000},"user":{"username":"","email":"","first_name":"","last_name":"","is_active":false,"is_org_admin":false,"is_internal":false,"locale":"","user_id":""},"system":{},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"","issuer_dn":""},"type":"Internal","auth_type":"basic-auth"}}
//Output: {"identity":{"org_id":"03797","internal":{"org_id":"08321","auth_time":-2978345600000000000},"user":{"username":"","email":"","first_name":"","last_name":"","is_active":false,"is_org_admin":false,"is_internal":false,"locale":"","user_id":""},"system":{},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"","issuer_dn":""},"service_account":{"client_id":"","username":""},"type":"Internal","auth_type":"basic-auth"},"entitlements":null}
}

func ExampleSystem() {
Expand All @@ -46,7 +46,7 @@ func ExampleSystem() {
panic(err)
}
fmt.Println(string(data))
//Output: {"identity":{"account_number":"16398","org_id":"57572","internal":{"org_id":"57572"},"user":{"username":"","email":"","first_name":"","last_name":"","is_active":false,"is_org_admin":false,"is_internal":false,"locale":"","user_id":""},"system":{"cn":"It6P","cert_type":"consumer","cluster_id":"x8LdjPo"},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"","issuer_dn":""},"type":"System","auth_type":"basic-auth"}}
//Output: {"identity":{"account_number":"16398","org_id":"57572","internal":{"org_id":"57572"},"user":{"username":"","email":"","first_name":"","last_name":"","is_active":false,"is_org_admin":false,"is_internal":false,"locale":"","user_id":""},"system":{"cn":"It6P","cert_type":"consumer","cluster_id":"x8LdjPo"},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"","issuer_dn":""},"service_account":{"client_id":"","username":""},"type":"System","auth_type":"basic-auth"},"entitlements":null}
}

func ExampleUser() {
Expand All @@ -60,7 +60,7 @@ func ExampleUser() {
panic(err)
}
fmt.Println(string(data))
//Output: {"identity":{"org_id":"23807","internal":{"org_id":"23807"},"user":{"username":"tycoon","email":"[email protected]","first_name":"Frankie","last_name":"Collins","is_active":false,"is_org_admin":false,"is_internal":true,"locale":"pi","user_id":"backset"},"system":{},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"","issuer_dn":""},"type":"User","auth_type":"cert-auth"}}
//Output: {"identity":{"org_id":"23807","internal":{"org_id":"23807"},"user":{"username":"tycoon","email":"[email protected]","first_name":"Frankie","last_name":"Collins","is_active":false,"is_org_admin":false,"is_internal":true,"locale":"pi","user_id":"backset"},"system":{},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"","issuer_dn":""},"service_account":{"client_id":"","username":""},"type":"User","auth_type":"cert-auth"},"entitlements":null}
}

func ExampleX509() {
Expand All @@ -74,5 +74,19 @@ func ExampleX509() {
panic(err)
}
fmt.Println(string(data))
//Output: {"identity":{"org_id":"23807","internal":{"org_id":""},"user":{"username":"","email":"","first_name":"","last_name":"","is_active":false,"is_org_admin":false,"is_internal":false,"locale":"","user_id":""},"system":{},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"2","issuer_dn":"3sfSj"},"type":"X509","auth_type":"cert-auth"}}
//Output: {"identity":{"org_id":"23807","internal":{"org_id":""},"user":{"username":"","email":"","first_name":"","last_name":"","is_active":false,"is_org_admin":false,"is_internal":false,"locale":"","user_id":""},"system":{},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"2","issuer_dn":"3sfSj"},"service_account":{"client_id":"","username":""},"type":"X509","auth_type":"cert-auth"},"entitlements":null}
}

func ExampleServiceAccount() {
xrhidgen.SetSeed(103)
id, err := xrhidgen.NewServiceAccountIdentity(xrhidgen.Identity{}, xrhidgen.ServiceAccount{})
if err != nil {
panic(err)
}
data, err := json.Marshal(id)
if err != nil {
panic(err)
}
fmt.Println(string(data))
//Output: {"identity":{"org_id":"23807","internal":{"org_id":""},"user":{"username":"","email":"","first_name":"","last_name":"","is_active":false,"is_org_admin":false,"is_internal":false,"locale":"","user_id":""},"system":{},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"","issuer_dn":""},"service_account":{"client_id":"2","username":"crump"},"type":"ServiceAccount","auth_type":"cert-auth"},"entitlements":null}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/peterbourgon/ff/v3 v3.4.0
github.com/pioz/faker v1.7.3
github.com/redhatinsights/platform-go-middlewares v0.20.0
github.com/redhatinsights/platform-go-middlewares/v2 v2.0.0-20240118174021-7836c2be5281
github.com/sgreben/flagvar v1.10.1
go.openly.dev/pointy v1.3.0
)
Expand Down
Loading