2018-05-16 18:59:35 +00:00
package main
import (
"context"
"os"
2019-01-08 00:37:16 +00:00
platform "github.com/influxdata/influxdb"
"github.com/influxdata/influxdb/cmd/influx/internal"
"github.com/influxdata/influxdb/http"
2018-05-16 18:59:35 +00:00
"github.com/spf13/cobra"
)
2020-01-10 00:34:30 +00:00
func cmdAuth ( ) * cobra . Command {
cmd := & cobra . Command {
Use : "auth" ,
Aliases : [ ] string { "authorization" } ,
Short : "Authorization management commands" ,
Run : seeHelp ,
}
cmd . AddCommand (
authActiveCmd ( ) ,
authCreateCmd ( ) ,
authDeleteCmd ( ) ,
authFindCmd ( ) ,
authInactiveCmd ( ) ,
)
return cmd
}
var authCreateFlags struct {
2018-05-16 18:59:35 +00:00
user string
2020-01-10 00:34:30 +00:00
org organization
2018-05-16 18:59:35 +00:00
2019-01-10 21:21:59 +00:00
writeUserPermission bool
readUserPermission bool
writeBucketsPermission bool
readBucketsPermission bool
2018-05-16 18:59:35 +00:00
writeBucketPermissions [ ] string
2019-01-10 21:21:59 +00:00
readBucketPermissions [ ] string
2019-01-10 23:38:07 +00:00
writeTasksPermission bool
readTasksPermission bool
writeTelegrafsPermission bool
readTelegrafsPermission bool
writeOrganizationsPermission bool
readOrganizationsPermission bool
writeDashboardsPermission bool
readDashboardsPermission bool
2019-07-25 15:05:05 +00:00
2019-08-12 17:20:59 +00:00
writeCheckPermission bool
readCheckPermission bool
2019-07-25 15:05:05 +00:00
writeNotificationRulePermission bool
readNotificationRulePermission bool
2019-08-12 17:20:59 +00:00
writeNotificationEndpointPermission bool
readNotificationEndpointPermission bool
2018-05-16 18:59:35 +00:00
}
2019-11-15 18:23:24 +00:00
func authCreateCmd ( ) * cobra . Command {
cmd := & cobra . Command {
2018-05-16 18:59:35 +00:00
Use : "create" ,
Short : "Create authorization" ,
2019-01-22 18:19:26 +00:00
RunE : wrapCheckSetup ( authorizationCreateF ) ,
2018-05-16 18:59:35 +00:00
}
2020-01-10 00:34:30 +00:00
authCreateFlags . org . register ( cmd , false )
2019-01-10 21:21:59 +00:00
2019-11-15 18:23:24 +00:00
cmd . Flags ( ) . StringVarP ( & authCreateFlags . user , "user" , "u" , "" , "The user name" )
2018-05-16 18:59:35 +00:00
2019-11-15 18:23:24 +00:00
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . writeUserPermission , "write-user" , "" , false , "Grants the permission to perform mutative actions against organization users" )
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . readUserPermission , "read-user" , "" , false , "Grants the permission to perform read actions against organization users" )
2019-01-10 21:21:59 +00:00
2019-11-15 18:23:24 +00:00
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . writeBucketsPermission , "write-buckets" , "" , false , "Grants the permission to perform mutative actions against organization buckets" )
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . readBucketsPermission , "read-buckets" , "" , false , "Grants the permission to perform read actions against organization buckets" )
2018-05-16 18:59:35 +00:00
2019-11-15 18:23:24 +00:00
cmd . Flags ( ) . StringArrayVarP ( & authCreateFlags . writeBucketPermissions , "write-bucket" , "" , [ ] string { } , "The bucket id" )
cmd . Flags ( ) . StringArrayVarP ( & authCreateFlags . readBucketPermissions , "read-bucket" , "" , [ ] string { } , "The bucket id" )
2018-05-16 18:59:35 +00:00
2019-11-15 18:23:24 +00:00
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . writeTasksPermission , "write-tasks" , "" , false , "Grants the permission to create tasks" )
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . readTasksPermission , "read-tasks" , "" , false , "Grants the permission to read tasks" )
2019-01-10 23:38:07 +00:00
2019-11-15 18:23:24 +00:00
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . writeTelegrafsPermission , "write-telegrafs" , "" , false , "Grants the permission to create telegraf configs" )
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . readTelegrafsPermission , "read-telegrafs" , "" , false , "Grants the permission to read telegraf configs" )
2019-01-10 23:38:07 +00:00
2019-11-15 18:23:24 +00:00
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . writeOrganizationsPermission , "write-orgs" , "" , false , "Grants the permission to create organizations" )
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . readOrganizationsPermission , "read-orgs" , "" , false , "Grants the permission to read organizations" )
2019-01-10 23:38:07 +00:00
2019-11-15 18:23:24 +00:00
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . writeDashboardsPermission , "write-dashboards" , "" , false , "Grants the permission to create dashboards" )
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . readDashboardsPermission , "read-dashboards" , "" , false , "Grants the permission to read dashboards" )
2019-01-10 23:38:07 +00:00
2019-11-15 18:23:24 +00:00
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . writeNotificationRulePermission , "write-notificationRules" , "" , false , "Grants the permission to create notificationRules" )
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . readNotificationRulePermission , "read-notificationRules" , "" , false , "Grants the permission to read notificationRules" )
2019-07-25 15:05:05 +00:00
2019-11-15 18:23:24 +00:00
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . writeNotificationEndpointPermission , "write-notificationEndpoints" , "" , false , "Grants the permission to create notificationEndpoints" )
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . readNotificationEndpointPermission , "read-notificationEndpoints" , "" , false , "Grants the permission to read notificationEndpoints" )
2019-08-12 17:20:59 +00:00
2019-11-15 18:23:24 +00:00
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . writeCheckPermission , "write-checks" , "" , false , "Grants the permission to create checks" )
cmd . Flags ( ) . BoolVarP ( & authCreateFlags . readCheckPermission , "read-checks" , "" , false , "Grants the permission to read checks" )
2019-08-12 17:20:59 +00:00
2019-11-15 18:23:24 +00:00
return cmd
2018-05-16 18:59:35 +00:00
}
2018-12-28 23:02:19 +00:00
func authorizationCreateF ( cmd * cobra . Command , args [ ] string ) error {
2020-01-10 00:34:30 +00:00
if err := authCreateFlags . org . validOrgFlags ( ) ; err != nil {
return err
}
2019-12-10 03:11:53 +00:00
orgSvc , err := newOrganizationService ( )
2019-01-10 21:21:59 +00:00
if err != nil {
return err
}
2020-01-10 00:34:30 +00:00
orgID , err := authCreateFlags . org . getID ( orgSvc )
2019-01-10 21:21:59 +00:00
if err != nil {
return err
}
2019-11-15 18:23:24 +00:00
bucketPerms := [ ] struct {
action platform . Action
perms [ ] string
} {
{ action : platform . ReadAction , perms : authCreateFlags . readBucketPermissions } ,
{ action : platform . WriteAction , perms : authCreateFlags . writeBucketPermissions } ,
2018-05-16 18:59:35 +00:00
}
2018-12-28 23:02:19 +00:00
2020-01-10 00:34:30 +00:00
var permissions [ ] platform . Permission
2019-11-15 18:23:24 +00:00
for _ , bp := range bucketPerms {
for _ , p := range bp . perms {
var id platform . ID
if err := id . DecodeFromString ( p ) ; err != nil {
return err
}
2018-12-28 23:02:19 +00:00
2020-01-10 00:34:30 +00:00
p , err := platform . NewPermissionAtID ( id , bp . action , platform . BucketsResourceType , orgID )
2019-11-15 18:23:24 +00:00
if err != nil {
return err
}
2018-05-16 18:59:35 +00:00
2019-11-15 18:23:24 +00:00
permissions = append ( permissions , * p )
2019-01-10 23:38:07 +00:00
}
}
2019-11-15 18:23:24 +00:00
providedPerm := [ ] struct {
readPerm , writePerm bool
ResourceType platform . ResourceType
} {
{
readPerm : authCreateFlags . readBucketsPermission ,
writePerm : authCreateFlags . writeBucketsPermission ,
ResourceType : platform . BucketsResourceType ,
} ,
{
readPerm : authCreateFlags . readCheckPermission ,
writePerm : authCreateFlags . writeCheckPermission ,
ResourceType : platform . ChecksResourceType ,
} ,
{
readPerm : authCreateFlags . readDashboardsPermission ,
writePerm : authCreateFlags . writeDashboardsPermission ,
ResourceType : platform . DashboardsResourceType ,
} ,
{
readPerm : authCreateFlags . readNotificationEndpointPermission ,
writePerm : authCreateFlags . writeNotificationEndpointPermission ,
ResourceType : platform . NotificationEndpointResourceType ,
} ,
{
readPerm : authCreateFlags . readNotificationRulePermission ,
writePerm : authCreateFlags . writeNotificationRulePermission ,
ResourceType : platform . NotificationRuleResourceType ,
} ,
{
readPerm : authCreateFlags . readOrganizationsPermission ,
writePerm : authCreateFlags . writeOrganizationsPermission ,
ResourceType : platform . OrgsResourceType ,
} ,
{
readPerm : authCreateFlags . readTasksPermission ,
writePerm : authCreateFlags . writeTasksPermission ,
ResourceType : platform . TasksResourceType ,
} ,
{
readPerm : authCreateFlags . readTelegrafsPermission ,
writePerm : authCreateFlags . writeTelegrafsPermission ,
ResourceType : platform . TelegrafsResourceType ,
} ,
{
readPerm : authCreateFlags . readUserPermission ,
writePerm : authCreateFlags . writeUserPermission ,
ResourceType : platform . UsersResourceType ,
} ,
}
for _ , provided := range providedPerm {
var actions [ ] platform . Action
if provided . readPerm {
actions = append ( actions , platform . ReadAction )
2019-01-10 23:38:07 +00:00
}
2019-11-15 18:23:24 +00:00
if provided . writePerm {
actions = append ( actions , platform . WriteAction )
2019-01-10 23:38:07 +00:00
}
2019-11-15 18:23:24 +00:00
for _ , action := range actions {
2020-01-10 00:34:30 +00:00
p , err := platform . NewPermission ( action , provided . ResourceType , orgID )
2019-11-15 18:23:24 +00:00
if err != nil {
return err
}
permissions = append ( permissions , * p )
2019-08-12 17:20:59 +00:00
}
}
2018-05-16 18:59:35 +00:00
authorization := & platform . Authorization {
Permissions : permissions ,
2020-01-10 00:34:30 +00:00
OrgID : orgID ,
2018-05-16 18:59:35 +00:00
}
2019-11-15 18:23:24 +00:00
if userName := authCreateFlags . user ; userName != "" {
2019-11-14 23:28:43 +00:00
userSvc , err := newUserService ( )
2019-07-16 17:25:13 +00:00
if err != nil {
return err
}
2020-01-10 00:34:30 +00:00
user , err := userSvc . FindUser ( context . Background ( ) , platform . UserFilter {
2019-07-16 17:25:13 +00:00
Name : & userName ,
} )
if err != nil {
return err
}
authorization . UserID = user . ID
}
2020-01-10 00:34:30 +00:00
s , err := newAuthorizationService ( )
2018-10-25 19:28:33 +00:00
if err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-05-16 18:59:35 +00:00
}
2018-10-25 19:28:33 +00:00
if err := s . CreateAuthorization ( context . Background ( ) , authorization ) ; err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-05-16 18:59:35 +00:00
}
w := internal . NewTabWriter ( os . Stdout )
w . WriteHeaders (
"ID" ,
"Token" ,
2018-08-28 17:58:38 +00:00
"Status" ,
2018-05-16 18:59:35 +00:00
"UserID" ,
"Permissions" ,
)
ps := [ ] string { }
for _ , p := range authorization . Permissions {
ps = append ( ps , p . String ( ) )
}
w . Write ( map [ string ] interface { } {
"ID" : authorization . ID . String ( ) ,
"Token" : authorization . Token ,
2018-08-28 17:58:38 +00:00
"Status" : authorization . Status ,
2018-05-16 18:59:35 +00:00
"UserID" : authorization . UserID . String ( ) ,
"Permissions" : ps ,
} )
2018-12-28 23:02:19 +00:00
2018-05-16 18:59:35 +00:00
w . Flush ( )
2018-12-28 23:02:19 +00:00
return nil
2018-05-16 18:59:35 +00:00
}
2020-01-10 00:34:30 +00:00
var authorizationFindFlags struct {
org organization
2018-05-16 18:59:35 +00:00
user string
userID string
id string
}
2019-11-15 18:23:24 +00:00
func authFindCmd ( ) * cobra . Command {
cmd := & cobra . Command {
2018-05-16 18:59:35 +00:00
Use : "find" ,
Short : "Find authorization" ,
2019-01-22 18:19:26 +00:00
RunE : wrapCheckSetup ( authorizationFindF ) ,
2018-05-16 18:59:35 +00:00
}
2019-11-15 18:23:24 +00:00
cmd . Flags ( ) . StringVarP ( & authorizationFindFlags . user , "user" , "u" , "" , "The user" )
cmd . Flags ( ) . StringVarP ( & authorizationFindFlags . userID , "user-id" , "" , "" , "The user ID" )
2019-12-17 19:55:35 +00:00
2019-11-15 18:23:24 +00:00
cmd . Flags ( ) . StringVarP ( & authorizationFindFlags . id , "id" , "i" , "" , "The authorization ID" )
2018-05-16 18:59:35 +00:00
2019-11-15 18:23:24 +00:00
return cmd
2018-05-16 18:59:35 +00:00
}
2020-01-10 00:34:30 +00:00
func newAuthorizationService ( ) ( platform . AuthorizationService , error ) {
2018-10-25 19:28:33 +00:00
if flags . local {
2019-03-28 08:42:38 +00:00
return newLocalKVService ( )
2018-10-25 19:28:33 +00:00
}
2019-12-17 19:56:04 +00:00
httpClient , err := newHTTPClient ( )
if err != nil {
return nil , err
}
2018-10-25 19:28:33 +00:00
return & http . AuthorizationService {
2019-12-17 19:56:04 +00:00
Client : httpClient ,
2018-10-25 19:28:33 +00:00
} , nil
}
2018-12-28 23:02:19 +00:00
func authorizationFindF ( cmd * cobra . Command , args [ ] string ) error {
2020-01-10 00:34:30 +00:00
s , err := newAuthorizationService ( )
2018-10-25 19:28:33 +00:00
if err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-05-16 18:59:35 +00:00
}
filter := platform . AuthorizationFilter { }
if authorizationFindFlags . id != "" {
2018-07-20 10:24:07 +00:00
fID , err := platform . IDFromString ( authorizationFindFlags . id )
2018-05-16 18:59:35 +00:00
if err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-05-16 18:59:35 +00:00
}
2018-07-20 10:24:07 +00:00
filter . ID = fID
2018-05-16 18:59:35 +00:00
}
if authorizationFindFlags . user != "" {
filter . User = & authorizationFindFlags . user
}
if authorizationFindFlags . userID != "" {
2018-07-20 10:24:07 +00:00
uID , err := platform . IDFromString ( authorizationFindFlags . userID )
2018-05-16 18:59:35 +00:00
if err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-05-16 18:59:35 +00:00
}
2018-07-20 10:24:07 +00:00
filter . UserID = uID
2018-05-16 18:59:35 +00:00
}
2020-01-10 00:34:30 +00:00
if authorizationFindFlags . org . name != "" {
filter . Org = & authorizationFindFlags . org . name
2019-04-14 09:06:03 +00:00
}
2020-01-10 00:34:30 +00:00
if authorizationFindFlags . org . id != "" {
oID , err := platform . IDFromString ( authorizationFindFlags . org . id )
2019-04-14 09:06:03 +00:00
if err != nil {
return err
}
filter . OrgID = oID
}
2018-05-16 18:59:35 +00:00
authorizations , _ , err := s . FindAuthorizations ( context . Background ( ) , filter )
if err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-05-16 18:59:35 +00:00
}
w := internal . NewTabWriter ( os . Stdout )
w . WriteHeaders (
"ID" ,
"Token" ,
2018-08-28 17:58:38 +00:00
"Status" ,
2018-05-16 18:59:35 +00:00
"User" ,
"UserID" ,
"Permissions" ,
)
for _ , a := range authorizations {
var permissions [ ] string
for _ , p := range a . Permissions {
permissions = append ( permissions , p . String ( ) )
}
w . Write ( map [ string ] interface { } {
"ID" : a . ID ,
"Token" : a . Token ,
2018-08-28 17:58:38 +00:00
"Status" : a . Status ,
2018-05-16 18:59:35 +00:00
"UserID" : a . UserID . String ( ) ,
"Permissions" : permissions ,
} )
}
2018-12-28 23:02:19 +00:00
2018-05-16 18:59:35 +00:00
w . Flush ( )
2018-12-28 23:02:19 +00:00
return nil
2018-05-16 18:59:35 +00:00
}
2020-01-10 00:34:30 +00:00
var authorizationDeleteFlags struct {
2018-05-16 18:59:35 +00:00
id string
}
2019-11-15 18:23:24 +00:00
func authDeleteCmd ( ) * cobra . Command {
cmd := & cobra . Command {
2018-05-16 18:59:35 +00:00
Use : "delete" ,
Short : "Delete authorization" ,
2019-01-22 18:19:26 +00:00
RunE : wrapCheckSetup ( authorizationDeleteF ) ,
2018-05-16 18:59:35 +00:00
}
2019-11-15 18:23:24 +00:00
cmd . Flags ( ) . StringVarP ( & authorizationDeleteFlags . id , "id" , "i" , "" , "The authorization ID (required)" )
cmd . MarkFlagRequired ( "id" )
2018-05-16 18:59:35 +00:00
2019-11-15 18:23:24 +00:00
return cmd
2018-05-16 18:59:35 +00:00
}
2018-12-28 23:02:19 +00:00
func authorizationDeleteF ( cmd * cobra . Command , args [ ] string ) error {
2020-01-10 00:34:30 +00:00
s , err := newAuthorizationService ( )
2018-10-25 19:28:33 +00:00
if err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-05-16 18:59:35 +00:00
}
2018-07-20 10:24:07 +00:00
id , err := platform . IDFromString ( authorizationDeleteFlags . id )
if err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-05-16 18:59:35 +00:00
}
ctx := context . TODO ( )
2018-07-20 10:24:07 +00:00
a , err := s . FindAuthorizationByID ( ctx , * id )
2018-05-16 18:59:35 +00:00
if err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-05-16 18:59:35 +00:00
}
2018-07-20 10:24:07 +00:00
if err := s . DeleteAuthorization ( context . Background ( ) , * id ) ; err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-05-16 18:59:35 +00:00
}
w := internal . NewTabWriter ( os . Stdout )
w . WriteHeaders (
"ID" ,
"Token" ,
"User" ,
"UserID" ,
"Permissions" ,
"Deleted" ,
)
ps := [ ] string { }
for _ , p := range a . Permissions {
ps = append ( ps , p . String ( ) )
}
w . Write ( map [ string ] interface { } {
"ID" : a . ID . String ( ) ,
"Token" : a . Token ,
"UserID" : a . UserID . String ( ) ,
"Permissions" : ps ,
"Deleted" : true ,
} )
2018-12-28 23:02:19 +00:00
2018-05-16 18:59:35 +00:00
w . Flush ( )
2018-12-28 23:02:19 +00:00
return nil
2018-05-16 18:59:35 +00:00
}
2018-08-27 19:29:50 +00:00
2020-01-10 00:34:30 +00:00
var authorizationActiveFlags struct {
2018-08-27 19:29:50 +00:00
id string
}
2019-11-15 18:23:24 +00:00
func authActiveCmd ( ) * cobra . Command {
cmd := & cobra . Command {
2018-08-28 17:58:38 +00:00
Use : "active" ,
2019-01-14 18:54:53 +00:00
Short : "Active authorization" ,
2019-01-22 18:19:26 +00:00
RunE : wrapCheckSetup ( authorizationActiveF ) ,
2018-08-27 19:29:50 +00:00
}
2019-11-15 18:23:24 +00:00
cmd . Flags ( ) . StringVarP ( & authorizationActiveFlags . id , "id" , "i" , "" , "The authorization ID (required)" )
cmd . MarkFlagRequired ( "id" )
2018-08-27 19:29:50 +00:00
2019-11-15 18:23:24 +00:00
return cmd
2018-08-27 19:29:50 +00:00
}
2018-12-28 23:02:19 +00:00
func authorizationActiveF ( cmd * cobra . Command , args [ ] string ) error {
2020-01-10 00:34:30 +00:00
s , err := newAuthorizationService ( )
2018-10-25 19:28:33 +00:00
if err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-08-27 19:29:50 +00:00
}
2018-09-27 11:00:25 +00:00
var id platform . ID
2018-08-28 17:58:38 +00:00
if err := id . DecodeFromString ( authorizationActiveFlags . id ) ; err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-08-27 19:29:50 +00:00
}
ctx := context . TODO ( )
2019-04-01 16:16:37 +00:00
if _ , err := s . FindAuthorizationByID ( ctx , id ) ; err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-08-27 19:29:50 +00:00
}
2019-04-01 16:16:37 +00:00
a , err := s . UpdateAuthorization ( context . Background ( ) , id , & platform . AuthorizationUpdate {
2019-03-27 19:02:45 +00:00
Status : platform . Active . Ptr ( ) ,
2019-04-01 16:16:37 +00:00
} )
if err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-08-27 19:29:50 +00:00
}
w := internal . NewTabWriter ( os . Stdout )
w . WriteHeaders (
"ID" ,
"Token" ,
2018-08-28 17:58:38 +00:00
"Status" ,
2018-08-27 19:29:50 +00:00
"User" ,
"UserID" ,
"Permissions" ,
)
ps := [ ] string { }
for _ , p := range a . Permissions {
ps = append ( ps , p . String ( ) )
}
w . Write ( map [ string ] interface { } {
"ID" : a . ID . String ( ) ,
"Token" : a . Token ,
2018-08-28 17:58:38 +00:00
"Status" : a . Status ,
2018-08-27 19:29:50 +00:00
"UserID" : a . UserID . String ( ) ,
"Permissions" : ps ,
} )
2018-12-28 23:02:19 +00:00
2018-08-27 19:29:50 +00:00
w . Flush ( )
2018-12-28 23:02:19 +00:00
return nil
2018-08-27 19:29:50 +00:00
}
2020-01-10 00:34:30 +00:00
var authorizationInactiveFlags struct {
2018-08-27 19:29:50 +00:00
id string
}
2019-11-15 18:23:24 +00:00
func authInactiveCmd ( ) * cobra . Command {
cmd := & cobra . Command {
2018-08-28 17:58:38 +00:00
Use : "inactive" ,
2019-01-14 18:54:53 +00:00
Short : "Inactive authorization" ,
2019-01-22 18:19:26 +00:00
RunE : wrapCheckSetup ( authorizationInactiveF ) ,
2018-08-27 19:29:50 +00:00
}
2019-11-15 18:23:24 +00:00
cmd . Flags ( ) . StringVarP ( & authorizationInactiveFlags . id , "id" , "i" , "" , "The authorization ID (required)" )
cmd . MarkFlagRequired ( "id" )
2018-08-27 19:29:50 +00:00
2019-11-15 18:23:24 +00:00
return cmd
2018-08-27 19:29:50 +00:00
}
2018-12-28 23:02:19 +00:00
func authorizationInactiveF ( cmd * cobra . Command , args [ ] string ) error {
2020-01-10 00:34:30 +00:00
s , err := newAuthorizationService ( )
2018-10-25 19:28:33 +00:00
if err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-08-27 19:29:50 +00:00
}
2018-09-27 11:00:25 +00:00
var id platform . ID
2018-08-28 17:58:38 +00:00
if err := id . DecodeFromString ( authorizationInactiveFlags . id ) ; err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-08-27 19:29:50 +00:00
}
ctx := context . TODO ( )
2019-04-01 16:16:37 +00:00
if _ , err = s . FindAuthorizationByID ( ctx , id ) ; err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-08-27 19:29:50 +00:00
}
2019-04-01 16:16:37 +00:00
a , err := s . UpdateAuthorization ( context . Background ( ) , id , & platform . AuthorizationUpdate {
2019-03-27 19:02:45 +00:00
Status : platform . Inactive . Ptr ( ) ,
2019-04-01 16:16:37 +00:00
} )
if err != nil {
2018-12-28 23:02:19 +00:00
return err
2018-08-27 19:29:50 +00:00
}
w := internal . NewTabWriter ( os . Stdout )
w . WriteHeaders (
"ID" ,
"Token" ,
2018-08-28 17:58:38 +00:00
"Status" ,
2018-08-27 19:29:50 +00:00
"User" ,
"UserID" ,
"Permissions" ,
)
ps := [ ] string { }
for _ , p := range a . Permissions {
ps = append ( ps , p . String ( ) )
}
w . Write ( map [ string ] interface { } {
"ID" : a . ID . String ( ) ,
"Token" : a . Token ,
2018-08-28 17:58:38 +00:00
"Status" : a . Status ,
2018-08-27 19:29:50 +00:00
"UserID" : a . UserID . String ( ) ,
"Permissions" : ps ,
} )
2018-12-28 23:02:19 +00:00
2018-08-27 19:29:50 +00:00
w . Flush ( )
2018-12-28 23:02:19 +00:00
return nil
2018-08-27 19:29:50 +00:00
}