Introduce service accounts and bootstrap tokens (#22)

This commit is contained in:
Nikolay Edigaryev
2023-02-21 11:34:12 -05:00
committed by GitHub
parent edb9b3d693
commit 8df31f7c2d
37 changed files with 1245 additions and 40 deletions
+53
View File
@@ -0,0 +1,53 @@
package get
import (
"fmt"
"github.com/cirruslabs/orchard/internal/bootstraptoken"
"github.com/cirruslabs/orchard/internal/config"
"github.com/cirruslabs/orchard/pkg/client"
"github.com/spf13/cobra"
)
func newGetBootstrapTokenCommand() *cobra.Command {
command := &cobra.Command{
Use: "bootstrap-token",
Short: "Retrieve a bootstrap token for the specified service account",
RunE: runGetBootstrapToken,
Args: cobra.ExactArgs(1),
}
return command
}
func runGetBootstrapToken(cmd *cobra.Command, args []string) error {
name := args[0]
configHandle, err := config.NewHandle()
if err != nil {
return err
}
defaultContext, err := configHandle.DefaultContext()
if err != nil {
return err
}
client, err := client.New()
if err != nil {
return err
}
serviceAccount, err := client.ServiceAccounts().Get(cmd.Context(), name)
if err != nil {
return err
}
bootstrapToken, err := bootstraptoken.New(defaultContext.Certificate, serviceAccount.Name, serviceAccount.Token)
if err != nil {
return err
}
fmt.Println(bootstrapToken)
return nil
}
+19
View File
@@ -0,0 +1,19 @@
package get
import (
"errors"
"github.com/spf13/cobra"
)
var ErrGetFailed = errors.New("get command failed")
func NewCommand() *cobra.Command {
command := &cobra.Command{
Use: "get",
Short: "Retrieve resources from the controller",
}
command.AddCommand(newGetServiceAccountCommand(), newGetBootstrapTokenCommand())
return command
}
+70
View File
@@ -0,0 +1,70 @@
package get
import (
"fmt"
"github.com/cirruslabs/orchard/internal/structpath"
"github.com/cirruslabs/orchard/pkg/client"
"github.com/gosuri/uitable"
"github.com/spf13/cobra"
"strings"
)
func newGetServiceAccountCommand() *cobra.Command {
command := &cobra.Command{
Use: "service-account",
Short: "Retrieve service account and it's fields",
RunE: runGetServiceAccount,
Args: cobra.ExactArgs(1),
}
return command
}
func runGetServiceAccount(cmd *cobra.Command, args []string) error {
name := args[0]
client, err := client.New()
if err != nil {
return err
}
// Ability to retrieve resource fields (e.g. "orchard get service-account workers/token")
splits := strings.Split(name, "/")
var path []string
if len(splits) > 1 {
name = splits[0]
path = splits[1:]
}
serviceAccount, err := client.ServiceAccounts().Get(cmd.Context(), name)
if err != nil {
return err
}
// Ability to retrieve resource fields (e.g. "orchard get service-account workers/token")
if len(path) != 0 {
result, ok := structpath.Lookup(*serviceAccount, path)
if !ok {
return fmt.Errorf("%w: failed to find the specified field \"%s\" or the field is not a string",
ErrGetFailed, strings.Join(path, "/"))
}
fmt.Println(result)
return nil
}
table := uitable.New()
table.AddRow("name", serviceAccount.Name)
var scopeList []string
for _, scope := range serviceAccount.Roles {
scopeList = append(scopeList, string(scope))
}
table.AddRow("roles", strings.Join(scopeList, ", "))
fmt.Println(table)
return nil
}