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

Feat: token ttl #474

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
65 changes: 49 additions & 16 deletions commands/token/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,72 @@ import (
"errors"
"fmt"

"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/jsontabwriter"
"github.com/ionos-cloud/ionosctl/v6/internal/utils"
authservice "github.com/ionos-cloud/ionosctl/v6/services/auth-v1"
"github.com/spf13/viper"
)

func TokenPostCmd() *core.Command {
cmd := core.NewCommand(context.Background(), nil, core.CommandBuilder{
Namespace: "token",
Resource: "token",
Verb: "generate",
Aliases: []string{"create"},
ShortDesc: "Create a new Token",
LongDesc: "Use this command to generate a new Token. Only the JSON Web Token, associated with user credentials, will be displayed.",
Example: generateTokenExample,
PreCmdRun: core.NoPreRun,
CmdRun: runTokenCreate,
InitClient: true,
})
cmd.AddIntFlag(authservice.ArgContractNo, "", 0, "Users with multiple contracts can provide the contract number, for which the token is generated")
cmd := core.NewCommand(
context.Background(), nil, core.CommandBuilder{
Namespace: "token",
Resource: "token",
Verb: "generate",
Aliases: []string{"create"},
ShortDesc: "Create a new Token",
LongDesc: "Use this command to generate a new Token. Only the JSON Web Token, associated with user credentials, will be displayed.",
Example: generateTokenExample,
PreCmdRun: core.NoPreRun,
CmdRun: runTokenCreate,
InitClient: true,
},
)
cmd.AddIntFlag(
authservice.ArgContractNo, "", 0,
"Users with multiple contracts can provide the contract number, for which the token is generated",
)
cmd.AddSetFlag(
constants.FlagTtl, "", "1Y", []string{"1h", "4h", "1D", "7D", "1M", "2M", "3M", "6M", "1Y"},
"Token Time to Live in seconds",
)

return cmd
}

func runTokenCreate(c *core.CommandConfig) error {
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput("Generating new token.."))

var contractNumber int32
if viper.IsSet(core.GetFlagName(c.NS, authservice.ArgContractNo)) {
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(contractNumberMessage,
viper.GetInt32(core.GetFlagName(c.NS, authservice.ArgContractNo))))
fmt.Fprintf(
c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(
contractNumberMessage,
viper.GetInt32(core.GetFlagName(c.NS, authservice.ArgContractNo)),
),
)
contractNumber = viper.GetInt32(core.GetFlagName(c.NS, authservice.ArgContractNo))
}

newJwt, _, err := c.AuthV1Services.Tokens().Create(viper.GetInt32(core.GetFlagName(c.NS, authservice.ArgContractNo)))
var ttl int
var err error
if viper.IsSet(core.GetFlagName(c.NS, constants.FlagTtl)) {
fmt.Fprintf(
c.Command.Command.ErrOrStderr(),
jsontabwriter.GenerateVerboseOutput(
"Token TTL: %v",
viper.GetString(core.GetFlagName(c.NS, constants.FlagTtl)),
),
)

ttl, err = utils.ConvertTime(viper.GetString(core.GetFlagName(c.NS, constants.FlagTtl)), utils.Seconds)
if err != nil {
return err
}
}
newJwt, _, err := c.AuthV1Services.Tokens().Create(contractNumber, int32(ttl))
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions commands/token/token_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func testCreateToken(t *testing.T) {

tokFirstCreationTime = time.Now().In(time.UTC)
c := token.TokenPostCmd()
c.Command.Flags().Set(constants.FlagTtl, "1h")
err = c.Command.Execute()
assert.NoError(t, err)

Expand Down Expand Up @@ -109,6 +110,7 @@ func testCreateToken(t *testing.T) {
buff := bytes.NewBuffer([]byte{})
c = token.TokenPostCmd()
c.Command.SetOut(buff)
c.Command.Flags().Set(constants.FlagTtl, "1h")
err = c.Command.Execute()
assert.NoError(t, err)

Expand Down
1 change: 1 addition & 0 deletions docs/subcommands/Authentication/token/generate.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Use this command to generate a new Token. Only the JSON Web Token, associated wi
--no-headers Don't print table headers when table output is used
-o, --output string Desired output format [text|json|api-json] (default "text")
-q, --quiet Quiet output
--ttl string Token Time to Live in seconds. Can be one of: 1h, 4h, 1D, 7D, 1M, 2M, 3M, 6M, 1Y (default "1Y")
-v, --verbose Print step-by-step process when running command
```

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/fatih/structs v1.1.0
github.com/gofrs/uuid/v5 v5.0.0
github.com/golang/mock v1.6.0
github.com/ionos-cloud/sdk-go-auth v1.0.6
github.com/ionos-cloud/sdk-go-auth v1.0.10
github.com/ionos-cloud/sdk-go-container-registry v1.1.0
github.com/ionos-cloud/sdk-go-dataplatform v1.0.2
github.com/ionos-cloud/sdk-go-dbaas-mongo v1.3.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/ionos-cloud/sdk-go-auth v1.0.6 h1:N7XQ8JLKM9OS1DDlDCIN3Nk4RV4MuNvHFd50evuWgSM=
github.com/ionos-cloud/sdk-go-auth v1.0.6/go.mod h1:4MuUIZbnQjgNalEB9LUzIuBTI1keqgA7EpJsjEvMAls=
github.com/ionos-cloud/sdk-go-auth v1.0.10 h1:aHWstSqtmWZxvNVEye42MBK9WcbTQnbc3GBgPj6tmcE=
github.com/ionos-cloud/sdk-go-auth v1.0.10/go.mod h1:yVHBsuAWLbcnlgfviCDZ5HE7k3KwkmK7GjgaAvnnKxI=
github.com/ionos-cloud/sdk-go-cdn v1.1.0 h1:bwtrey2m1mElQV9TlhzR39mC6IMeWF2P5ztd6cmRKfw=
github.com/ionos-cloud/sdk-go-cdn v1.1.0/go.mod h1:W2XZ5CYhKROyvtLaz3VgTKe+nXI1+NZD1gE/YliHUeU=
github.com/ionos-cloud/sdk-go-cert-manager v1.0.1 h1:QGnenRREavvU0ZrXvY4hmCMXRyvmKj5jaXsFX9Apfd0=
Expand Down
Loading
Loading