mirror of https://github.com/harness/drone.git
[MAINT] Add PAT CLI command and update readme (#45)
Add PAT CLI command and update readmejobatzil/rename
parent
c0258b34ef
commit
2d897c4486
59
README.md
59
README.md
|
@ -23,8 +23,8 @@ $ popd
|
||||||
Build the server and command line tools:
|
Build the server and command line tools:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ go generate ./...
|
# STANDALONE
|
||||||
$ go build -o release/gitness
|
$ make build
|
||||||
```
|
```
|
||||||
|
|
||||||
# Test
|
# Test
|
||||||
|
@ -42,7 +42,8 @@ This project supports all operating systems and architectures supported by Go.
|
||||||
Start the server at `localhost:3000`
|
Start the server at `localhost:3000`
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ release/gitness server
|
# STANDALONE
|
||||||
|
./gitness server .local.env
|
||||||
```
|
```
|
||||||
|
|
||||||
# User Interface
|
# User Interface
|
||||||
|
@ -53,60 +54,70 @@ This project includes a simple user interface for interacting with the system. W
|
||||||
|
|
||||||
This project includes a swagger specification. When you run the application, you can access the swagger specification by navigating to `http://localhost:3000/swagger` in your browser.
|
This project includes a swagger specification. When you run the application, you can access the swagger specification by navigating to `http://localhost:3000/swagger` in your browser.
|
||||||
|
|
||||||
# Command Line
|
# CLI
|
||||||
|
|
||||||
This project includes simple command line tools for interacting with the system. Please remember that you must start the server before you can execute commands.
|
This project includes simple command line tools for interacting with the system. Please remember that you must start the server before you can execute commands.
|
||||||
|
|
||||||
Register a new user:
|
Register a new user:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ release/gitness register
|
$ ./gitness register
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> NOTE: A user `admin` (pw: `changeit`) gets created by default.
|
||||||
|
|
||||||
|
|
||||||
Login to the application:
|
Login to the application:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ release/gitness login
|
$ ./gitness login
|
||||||
```
|
```
|
||||||
|
|
||||||
Logout from the application:
|
Logout from the application:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ release/gitness logout
|
$ ./gitness logout
|
||||||
```
|
```
|
||||||
|
|
||||||
View your account details:
|
View your account details:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ release/gitness account
|
$ ./gitness user self
|
||||||
```
|
```
|
||||||
|
|
||||||
Generate a personal access token:
|
Generate a personal access token:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ release/gitness token
|
$ ./gitness user pat $NAME $LIFETIME_IN_S
|
||||||
```
|
|
||||||
|
|
||||||
Create a pipeline:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ release/gitness pipeline create <name>
|
|
||||||
```
|
|
||||||
|
|
||||||
List pipelines:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ release/gitness pipeline ls
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Debug and output http responses from the server:
|
Debug and output http responses from the server:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ DEBUG=true release/gitness pipeline ls
|
$ DEBUG=true ./gitness user self
|
||||||
```
|
```
|
||||||
|
|
||||||
View all commands:
|
View all commands:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ release/gitness --help
|
$ ./gitness --help
|
||||||
|
```
|
||||||
|
|
||||||
|
# REST API
|
||||||
|
Please refer to the swagger for the specification of our rest API.
|
||||||
|
|
||||||
|
For testing, it's simplest to execute operations as the default user `admin` using a PAT:
|
||||||
|
```bash
|
||||||
|
# LOGIN (user: admin, pw: changeit)
|
||||||
|
$ ./gitness login
|
||||||
|
|
||||||
|
# GENERATE PAT (1 YEAR VALIDITY)
|
||||||
|
$ ./gitness user pat mypat 2592000
|
||||||
|
```
|
||||||
|
|
||||||
|
The command outputs a valid PAT that has been granted full access as the user.
|
||||||
|
The token can then be send as part of the `Authorization` header with Postman or curl:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ curl http://localhost:3000/api/v1/user \
|
||||||
|
-H "Authorization: Bearer $TOKEN"
|
||||||
```
|
```
|
|
@ -0,0 +1,86 @@
|
||||||
|
// Copyright 2022 Harness Inc. All rights reserved.
|
||||||
|
// Use of this source code is governed by the Polyform Free Trial License
|
||||||
|
// that can be found in the LICENSE.md file for this repository.
|
||||||
|
|
||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
"text/template"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/harness/gitness/cli/util"
|
||||||
|
"github.com/harness/gitness/internal/api/controller/user"
|
||||||
|
"github.com/harness/gitness/types/enum"
|
||||||
|
|
||||||
|
"github.com/drone/funcmap"
|
||||||
|
"gopkg.in/alecthomas/kingpin.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
const tokenTmpl = `
|
||||||
|
name: {{ .Token.Name }}
|
||||||
|
expiresAt: {{ .Token.ExpiresAt }}
|
||||||
|
token: {{ .AccessToken }}
|
||||||
|
` //#nosec G101
|
||||||
|
|
||||||
|
type createPATCommand struct {
|
||||||
|
name string
|
||||||
|
lifetimeInS int64
|
||||||
|
|
||||||
|
json bool
|
||||||
|
tmpl string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *createPATCommand) run(*kingpin.ParseContext) error {
|
||||||
|
client, err := util.Client()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
in := user.CreateTokenInput{
|
||||||
|
Name: c.name,
|
||||||
|
Lifetime: time.Duration(int64(time.Second) * c.lifetimeInS),
|
||||||
|
Grants: enum.AccessGrantAll,
|
||||||
|
}
|
||||||
|
|
||||||
|
tokenResp, err := client.UserCreatePAT(ctx, in)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if c.json {
|
||||||
|
enc := json.NewEncoder(os.Stdout)
|
||||||
|
enc.SetIndent("", " ")
|
||||||
|
return enc.Encode(tokenResp)
|
||||||
|
}
|
||||||
|
tmpl, err := template.New("_").Funcs(funcmap.Funcs).Parse(c.tmpl)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return tmpl.Execute(os.Stdout, tokenResp)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register the command.
|
||||||
|
func registerCreatePAT(app *kingpin.CmdClause) {
|
||||||
|
c := new(createPATCommand)
|
||||||
|
|
||||||
|
cmd := app.Command("pat", "create personal access token").
|
||||||
|
Action(c.run)
|
||||||
|
|
||||||
|
cmd.Arg("name", "the name of the token").
|
||||||
|
Required().StringVar(&c.name)
|
||||||
|
|
||||||
|
cmd.Arg("lifetime", "the lifetime of the token in seconds").
|
||||||
|
Required().Int64Var(&c.lifetimeInS)
|
||||||
|
|
||||||
|
cmd.Flag("json", "json encode the output").
|
||||||
|
BoolVar(&c.json)
|
||||||
|
|
||||||
|
cmd.Flag("format", "format the output using a Go template").
|
||||||
|
Default(tokenTmpl).
|
||||||
|
Hidden().
|
||||||
|
StringVar(&c.tmpl)
|
||||||
|
}
|
|
@ -53,10 +53,10 @@ func (c *command) run(*kingpin.ParseContext) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register the command.
|
// Register the command.
|
||||||
func Register(app *kingpin.Application) {
|
func registerSelf(app *kingpin.CmdClause) {
|
||||||
c := new(command)
|
c := new(command)
|
||||||
|
|
||||||
cmd := app.Command("account", "display authenticated user").
|
cmd := app.Command("self", "display authenticated user").
|
||||||
Action(c.run)
|
Action(c.run)
|
||||||
|
|
||||||
cmd.Flag("json", "json encode the output").
|
cmd.Flag("json", "json encode the output").
|
|
@ -0,0 +1,14 @@
|
||||||
|
// Copyright 2022 Harness Inc. All rights reserved.
|
||||||
|
// Use of this source code is governed by the Polyform Free Trial License
|
||||||
|
// that can be found in the LICENSE.md file for this repository.
|
||||||
|
|
||||||
|
package user
|
||||||
|
|
||||||
|
import "gopkg.in/alecthomas/kingpin.v2"
|
||||||
|
|
||||||
|
// Register the command.
|
||||||
|
func Register(app *kingpin.Application) {
|
||||||
|
cmd := app.Command("user", "manage currently logged-in user")
|
||||||
|
registerSelf(cmd)
|
||||||
|
registerCreatePAT(cmd)
|
||||||
|
}
|
|
@ -8,7 +8,7 @@ import "gopkg.in/alecthomas/kingpin.v2"
|
||||||
|
|
||||||
// Register the command.
|
// Register the command.
|
||||||
func Register(app *kingpin.Application) {
|
func Register(app *kingpin.Application) {
|
||||||
cmd := app.Command("user", "manage users")
|
cmd := app.Command("users", "manage users")
|
||||||
registerFind(cmd)
|
registerFind(cmd)
|
||||||
registerList(cmd)
|
registerList(cmd)
|
||||||
registerCreate(cmd)
|
registerCreate(cmd)
|
||||||
|
|
|
@ -16,6 +16,7 @@ import (
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
|
"github.com/harness/gitness/internal/api/controller/user"
|
||||||
"github.com/harness/gitness/types"
|
"github.com/harness/gitness/types"
|
||||||
"github.com/harness/gitness/version"
|
"github.com/harness/gitness/version"
|
||||||
)
|
)
|
||||||
|
@ -94,6 +95,14 @@ func (c *HTTPClient) Self(ctx context.Context) (*types.User, error) {
|
||||||
return out, err
|
return out, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UserCreatePAT creates a new PAT for the user.
|
||||||
|
func (c *HTTPClient) UserCreatePAT(ctx context.Context, in user.CreateTokenInput) (*types.TokenResponse, error) {
|
||||||
|
out := new(types.TokenResponse)
|
||||||
|
uri := fmt.Sprintf("%s/api/v1/user/tokens", c.base)
|
||||||
|
err := c.post(ctx, uri, in, out)
|
||||||
|
return out, err
|
||||||
|
}
|
||||||
|
|
||||||
// User returns a user by ID or email.
|
// User returns a user by ID or email.
|
||||||
func (c *HTTPClient) User(ctx context.Context, key string) (*types.User, error) {
|
func (c *HTTPClient) User(ctx context.Context, key string) (*types.User, error) {
|
||||||
out := new(types.User)
|
out := new(types.User)
|
||||||
|
|
|
@ -15,7 +15,7 @@ const (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrTokenLifeTimeOutOfBounds = &ValidationError{
|
ErrTokenLifeTimeOutOfBounds = &ValidationError{
|
||||||
"The life time of a token has to be between 1 second and 365 days.",
|
"The life time of a token has to be between 1 day and 365 days.",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue