2020-07-14 14:32:47 -07:00
package roles
import (
"fmt"
"strings"
"time"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/api/roles"
"github.com/hashicorp/boundary/internal/cmd/base"
2020-08-13 11:20:22 -04:00
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/internal/types/resource"
2020-07-14 14:32:47 -07:00
"github.com/mitchellh/go-wordwrap"
)
func principalsGrantsSynopsisFunc ( inFunc string , principals bool ) string {
var in string
switchStr := "principals (users, groups)"
if ! principals {
switchStr = "grants"
}
switch {
case strings . HasPrefix ( inFunc , "add" ):
in = fmt . Sprintf ( "Add %s to" , switchStr )
case strings . HasPrefix ( inFunc , "set" ):
in = fmt . Sprintf ( "Set the full contents of the %s on" , switchStr )
case strings . HasPrefix ( inFunc , "remove" ):
in = fmt . Sprintf ( "Remove %s from" , switchStr )
}
2020-08-10 17:15:02 -04:00
return wordwrap . WrapString ( fmt . Sprintf ( "%s a role within Boundary" , in ), base . TermWidth )
2020-07-14 14:32:47 -07:00
}
func addPrincipalsHelp () string {
return base . WrapForHelpText ([] string {
2020-08-10 17:15:02 -04:00
"Usage: boundary roles add-principals [options] [args]" ,
2020-07-14 14:32:47 -07:00
"" ,
2020-08-13 17:35:55 -04:00
` Adds principals (users, groups) to a role given its ID. The "principal" flag can be specified multiple times. Example:` ,
2020-07-14 14:32:47 -07:00
"" ,
2020-08-13 17:35:55 -04:00
` $ boundary roles add-principals -id r_1234567890 -principal u_1234567890` ,
2020-07-14 14:32:47 -07:00
})
}
func setPrincipalsHelp () string {
return base . WrapForHelpText ([] string {
2020-08-10 17:15:02 -04:00
"Usage: boundary roles set-principals [options] [args]" ,
2020-07-14 14:32:47 -07:00
"" ,
2020-08-13 17:35:55 -04:00
` Sets the complete set of principals (users, groups) on a role given its ID. The "principal" flag can be specified multiple times. Example:` ,
2020-07-14 14:32:47 -07:00
"" ,
2020-08-13 17:35:55 -04:00
` $ boundary roles set-principals -id r_1234567890 -principal u_anon -principal sg_1234567890` ,
2020-07-14 14:32:47 -07:00
})
}
func removePrincipalsHelp () string {
return base . WrapForHelpText ([] string {
2020-08-10 17:15:02 -04:00
"Usage: boundary roles remove-principals [options] [args]" ,
2020-07-14 14:32:47 -07:00
"" ,
2020-08-13 17:35:55 -04:00
` Removes principals (users, groups) from a role given its ID. The "principal" flags can be specified multiple times. Example:` ,
2020-07-14 14:32:47 -07:00
"" ,
2020-08-13 17:35:55 -04:00
` $ boundary roles remove-principals -id r_1234567890 -principal sg_1234567890` ,
2020-07-14 14:32:47 -07:00
})
}
func populateFlags ( c * Command , f * base . FlagSet , flagNames [] string ) {
2020-08-18 17:46:34 -04:00
common . PopulateCommonFlags ( c . Command , f , resource . Role . String (), flagNames )
2020-08-13 11:20:22 -04:00
2020-07-14 14:32:47 -07:00
for _ , name := range flagNames {
switch name {
case "grantscopeid" :
f . StringVar ( & base . StringVar {
Name : "grant-scope-id" ,
Target : & c . flagGrantScopeId ,
Usage : "The scope ID for grants set on the role" ,
})
case "principal" :
f . StringSliceVar ( & base . StringSliceVar {
Name : "principal" ,
Target : & c . flagPrincipals ,
Usage : "The principals (users or groups) to add, remove, or set. May be specified multiple times." ,
})
case "grant" :
f . StringSliceVar ( & base . StringSliceVar {
Name : "grant" ,
Target : & c . flagGrants ,
Usage : "The grants to add, remove, or set. May be specified multiple times. Can be in compact string format or JSON (be sure to escape JSON properly)." ,
})
}
}
}
2020-08-28 01:28:26 -04:00
func generateRoleTableOutput ( in * roles . Role ) string {
nonAttributeMap := map [ string ] interface {}{
"ID" : in . Id ,
"Version" : in . Version ,
2020-09-23 03:01:39 -04:00
"Created Time" : in . CreatedTime . Local (). Format ( time . RFC1123 ),
"Updated Time" : in . UpdatedTime . Local (). Format ( time . RFC1123 ),
2020-07-14 14:32:47 -07:00
}
2020-08-28 01:28:26 -04:00
if in . Name != "" {
nonAttributeMap [ "Name" ] = in . Name
2020-07-14 14:32:47 -07:00
}
2020-08-28 01:28:26 -04:00
if in . Description != "" {
nonAttributeMap [ "Description" ] = in . Description
2020-07-14 14:32:47 -07:00
}
2020-08-28 01:28:26 -04:00
if in . GrantScopeId != "" {
nonAttributeMap [ "Grant Scope ID" ] = in . GrantScopeId
2020-07-14 14:32:47 -07:00
}
2020-08-28 01:28:26 -04:00
2020-09-23 03:01:39 -04:00
maxLength := base . MaxAttributesLength ( nonAttributeMap , nil , nil )
2020-08-28 01:28:26 -04:00
2020-09-23 03:01:39 -04:00
ret := [] string {
"" ,
"Role information:" ,
base . WrapMap ( 2 , maxLength + 2 , nonAttributeMap ),
"" ,
" Scope:" ,
base . ScopeInfoForOutput ( in . Scope , maxLength ),
}
2020-08-28 01:28:26 -04:00
if len ( in . Principals ) > 0 {
ret = append ( ret ,
2020-10-01 01:49:20 -04:00
"" ,
2020-07-14 14:32:47 -07:00
fmt . Sprintf ( " Principals: %s" , "" ),
)
}
2020-08-28 01:28:26 -04:00
for _ , principal := range in . Principals {
ret = append ( ret ,
2020-07-14 14:32:47 -07:00
fmt . Sprintf ( " ID: %s" , principal . Id ),
fmt . Sprintf ( " Type: %s" , principal . Type ),
fmt . Sprintf ( " Scope ID: %s" , principal . ScopeId ),
)
}
2020-08-28 01:28:26 -04:00
if len ( in . Grants ) > 0 {
ret = append ( ret ,
2020-09-29 20:41:32 -04:00
"" ,
2020-07-14 14:32:47 -07:00
fmt . Sprintf ( " Canonical Grants: %s" , "" ),
)
}
2020-08-28 01:28:26 -04:00
for _ , grant := range in . Grants {
ret = append ( ret ,
2020-07-16 22:18:27 -04:00
fmt . Sprintf ( " %s" , grant . Canonical ),
2020-07-14 14:32:47 -07:00
)
}
2020-08-28 01:28:26 -04:00
return base . WrapForHelpText ( ret )
2020-07-14 14:32:47 -07:00
}