github.com/chenbh/concourse/v6@v6.4.2/skymarshal/skycmd/cf_flags.go (about)

     1  package skycmd
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  
     8  	"github.com/concourse/dex/connector/cf"
     9  	"github.com/concourse/flag"
    10  	multierror "github.com/hashicorp/go-multierror"
    11  )
    12  
    13  func init() {
    14  	RegisterConnector(&Connector{
    15  		id:         "cf",
    16  		config:     &CFFlags{},
    17  		teamConfig: &CFTeamFlags{},
    18  	})
    19  }
    20  
    21  type CFFlags struct {
    22  	ClientID           string      `long:"client-id" description:"(Required) Client id"`
    23  	ClientSecret       string      `long:"client-secret" description:"(Required) Client secret"`
    24  	APIURL             string      `long:"api-url" description:"(Required) The base API URL of your CF deployment. It will use this information to discover information about the authentication provider."`
    25  	CACerts            []flag.File `long:"ca-cert" description:"CA Certificate"`
    26  	InsecureSkipVerify bool        `long:"skip-ssl-validation" description:"Skip SSL validation"`
    27  }
    28  
    29  func (flag *CFFlags) Name() string {
    30  	return "CloudFoundry"
    31  }
    32  
    33  func (flag *CFFlags) Validate() error {
    34  	var errs *multierror.Error
    35  
    36  	if flag.APIURL == "" {
    37  		errs = multierror.Append(errs, errors.New("Missing api-url"))
    38  	}
    39  
    40  	if flag.ClientID == "" {
    41  		errs = multierror.Append(errs, errors.New("Missing client-id"))
    42  	}
    43  
    44  	if flag.ClientSecret == "" {
    45  		errs = multierror.Append(errs, errors.New("Missing client-secret"))
    46  	}
    47  
    48  	return errs.ErrorOrNil()
    49  }
    50  
    51  func (flag *CFFlags) Serialize(redirectURI string) ([]byte, error) {
    52  	if err := flag.Validate(); err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	caCerts := []string{}
    57  	for _, file := range flag.CACerts {
    58  		caCerts = append(caCerts, file.Path())
    59  	}
    60  
    61  	return json.Marshal(cf.Config{
    62  		ClientID:           flag.ClientID,
    63  		ClientSecret:       flag.ClientSecret,
    64  		APIURL:             flag.APIURL,
    65  		RootCAs:            caCerts,
    66  		InsecureSkipVerify: flag.InsecureSkipVerify,
    67  		RedirectURI:        redirectURI,
    68  	})
    69  }
    70  
    71  type CFTeamFlags struct {
    72  	Users            []string `long:"user" description:"A whitelisted CloudFoundry user" value-name:"USERNAME"`
    73  	Orgs             []string `long:"org" description:"A whitelisted CloudFoundry org" value-name:"ORG_NAME"`
    74  	Spaces           []string `long:"space" description:"(Deprecated) A whitelisted CloudFoundry space for users with the 'developer' role" value-name:"ORG_NAME:SPACE_NAME"`
    75  	SpacesAll        []string `long:"space-with-any-role" description:"A whitelisted CloudFoundry space for users with any role" value-name:"ORG_NAME:SPACE_NAME" mapstructure:"spaces_with_any_role"`
    76  	SpacesDeveloper  []string `long:"space-with-developer-role" description:"A whitelisted CloudFoundry space for users with the 'developer' role" value-name:"ORG_NAME:SPACE_NAME" mapstructure:"spaces_with_developer_role"`
    77  	SpacesAuditor    []string `long:"space-with-auditor-role" description:"A whitelisted CloudFoundry space for users with the 'auditor' role" value-name:"ORG_NAME:SPACE_NAME" mapstructure:"spaces_with_auditor_role"`
    78  	SpacesManager    []string `long:"space-with-manager-role" description:"A whitelisted CloudFoundry space for users with the 'manager' role" value-name:"ORG_NAME:SPACE_NAME" mapstructure:"spaces_with_manager_role"`
    79  	SpaceGuids       []string `long:"space-guid" description:"A whitelisted CloudFoundry space guid" value-name:"SPACE_GUID" mapstructure:"space_guids"`
    80  	SpaceGuidsLegacy []string `mapstructure:"spaceguids"`
    81  }
    82  
    83  func (flag *CFTeamFlags) GetUsers() []string {
    84  	return flag.Users
    85  }
    86  
    87  func (flag *CFTeamFlags) GetGroups() []string {
    88  	var groups []string
    89  	groups = append(groups, flag.Orgs...)
    90  	groups = append(groups, flag.SpacesAll...)
    91  	for _, space := range flag.Spaces {
    92  		groups = append(groups, fmt.Sprintf("%s:developer", space))
    93  	}
    94  	for _, space := range flag.SpacesDeveloper {
    95  		groups = append(groups, fmt.Sprintf("%s:developer", space))
    96  	}
    97  	for _, space := range flag.SpacesAuditor {
    98  		groups = append(groups, fmt.Sprintf("%s:auditor", space))
    99  	}
   100  	for _, space := range flag.SpacesManager {
   101  		groups = append(groups, fmt.Sprintf("%s:manager", space))
   102  	}
   103  	groups = append(groups, flag.SpaceGuids...)
   104  	groups = append(groups, flag.SpaceGuidsLegacy...)
   105  	return groups
   106  }