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 rotation support for user personal token #174

Open
wants to merge 2 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
25 changes: 25 additions & 0 deletions api/personal-tokens.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package api



type PersonalTokens struct {
client *Client
}

func (c *Client) PersonalTokens() *PersonalTokens { return &PersonalTokens{client: c} }

func (i *PersonalTokens) Create() (string, error) {
variables := map[string]interface{}{
}

var mutation struct {
ApiToken string `graphql:"createPersonalUserToken(input: {})"`
}

err := i.client.Mutate(&mutation, variables)
if err != nil {
return "", err
}

return mutation.ApiToken, nil
}
30 changes: 30 additions & 0 deletions cmd/humioctl/personal_tokens.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright © 2020 Humio Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"github.com/spf13/cobra"
)

func newPersonalTokensCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "personal-tokens",
Short: "Manage personal tokens",
}

cmd.AddCommand(newPersonalTokensRotateCmd())

return cmd
}
41 changes: 41 additions & 0 deletions cmd/humioctl/personal_tokens_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright © 2018 Humio Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"

"github.com/spf13/cobra"
)

func newPersonalTokensRotateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "rotate",
Short: "Rotate the personal token for current user",
Long: `Rotate the personal token for current user.

It will inherit the same permissions as the user.
It will create a new personal token, replacing existing one`,
Run: func(cmd *cobra.Command, args []string) {
client := NewApiClient(cmd)

personalToken, err := client.PersonalTokens().Create()
exitOnError(cmd, err, "Error generating new personal user token")

fmt.Fprintf(cmd.OutOrStdout(), "New personal user token generated: %s \n", personalToken)
},
}
return cmd
}
1 change: 1 addition & 0 deletions cmd/humioctl/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Common Management Commands:
rootCmd.AddCommand(newIngestCmd())
rootCmd.AddCommand(newProfilesCmd())
rootCmd.AddCommand(newIngestTokensCmd())
rootCmd.AddCommand(newPersonalTokensCmd())
rootCmd.AddCommand(newViewsCmd())
rootCmd.AddCommand(newCompletionCmd())
rootCmd.AddCommand(newLicenseCmd())
Expand Down