allow capi register in functional tests (#1384)
This commit is contained in:
parent
7ff67c2311
commit
caf1dc71fb
|
@ -20,6 +20,7 @@ import (
|
|||
|
||||
var CAPIURLPrefix string = "v2"
|
||||
var CAPIBaseURL string = "https://api.crowdsec.net/"
|
||||
var capiUserPrefix string
|
||||
|
||||
func NewCapiCmd() *cobra.Command {
|
||||
var cmdCapi = &cobra.Command{
|
||||
|
@ -46,8 +47,7 @@ func NewCapiCmd() *cobra.Command {
|
|||
DisableAutoGenTag: true,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
var err error
|
||||
|
||||
id, err := generateID()
|
||||
capiUser, err := generateID(capiUserPrefix)
|
||||
if err != nil {
|
||||
log.Fatalf("unable to generate machine id: %s", err)
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ func NewCapiCmd() *cobra.Command {
|
|||
log.Fatalf("unable to parse api url %s : %s", CAPIBaseURL, err)
|
||||
}
|
||||
_, err = apiclient.RegisterClient(&apiclient.Config{
|
||||
MachineID: id,
|
||||
MachineID: capiUser,
|
||||
Password: password,
|
||||
UserAgent: fmt.Sprintf("crowdsec/%s", cwversion.VersionStr()),
|
||||
URL: apiurl,
|
||||
|
@ -79,7 +79,7 @@ func NewCapiCmd() *cobra.Command {
|
|||
dumpFile = ""
|
||||
}
|
||||
apiCfg := csconfig.ApiCredentialsCfg{
|
||||
Login: id,
|
||||
Login: capiUser,
|
||||
Password: password.String(),
|
||||
URL: CAPIBaseURL,
|
||||
}
|
||||
|
@ -101,6 +101,8 @@ func NewCapiCmd() *cobra.Command {
|
|||
},
|
||||
}
|
||||
cmdCapiRegister.Flags().StringVarP(&outputFile, "file", "f", "", "output file destination")
|
||||
cmdCapiRegister.Flags().StringVar(&capiUserPrefix, "schmilblick", "", "set a schmilblick (use in tests only)")
|
||||
cmdCapiRegister.Flags().MarkHidden("schmilblick")
|
||||
cmdCapi.AddCommand(cmdCapiRegister)
|
||||
|
||||
var cmdCapiStatus = &cobra.Command{
|
||||
|
|
|
@ -51,7 +51,7 @@ Keep in mind the machine needs to be validated by an administrator on LAPI side
|
|||
Run: func(cmd *cobra.Command, args []string) {
|
||||
var err error
|
||||
if lapiUser == "" {
|
||||
lapiUser, err = generateID()
|
||||
lapiUser, err = generateID("")
|
||||
if err != nil {
|
||||
log.Fatalf("unable to generate machine id: %s", err)
|
||||
}
|
||||
|
|
|
@ -60,21 +60,36 @@ func generatePassword(length int) string {
|
|||
return string(buf)
|
||||
}
|
||||
|
||||
func generateID() (string, error) {
|
||||
id, err := machineid.ID()
|
||||
if err != nil {
|
||||
log.Debugf("failed to get machine-id with usual files : %s", err)
|
||||
// Returns a unique identifier for each crowdsec installation, using an
|
||||
// identifier of the OS installation where available, otherwise a random
|
||||
// string.
|
||||
func generateIDPrefix() (string, error) {
|
||||
prefix, err := machineid.ID()
|
||||
if err == nil {
|
||||
return prefix, nil
|
||||
}
|
||||
if id == "" || err != nil {
|
||||
log.Debugf("failed to get machine-id with usual files: %s", err)
|
||||
|
||||
bID, err := ioutil.ReadFile(uuid)
|
||||
if err != nil {
|
||||
if err == nil {
|
||||
return string(bID), nil
|
||||
}
|
||||
return "", errors.Wrap(err, "generating machine id")
|
||||
}
|
||||
|
||||
// Generate a unique identifier, composed by a prefix and a random suffix.
|
||||
// The prefix can be provided by a parameter to use in test environments.
|
||||
func generateID(prefix string) (string, error) {
|
||||
var err error
|
||||
if prefix == "" {
|
||||
prefix, err = generateIDPrefix()
|
||||
}
|
||||
id = string(bID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
id = strings.ReplaceAll(id, "-", "")[:32]
|
||||
id = fmt.Sprintf("%s%s", id, generatePassword(16))
|
||||
return id, nil
|
||||
prefix = strings.ReplaceAll(prefix, "-", "")[:32]
|
||||
suffix := generatePassword(16)
|
||||
return prefix + suffix, nil
|
||||
}
|
||||
|
||||
func NewMachinesCmd() *cobra.Command {
|
||||
|
@ -197,7 +212,7 @@ cscli machines add MyTestMachine --password MyPassword
|
|||
printHelp(cmd)
|
||||
return
|
||||
}
|
||||
machineID, err = generateID()
|
||||
machineID, err = generateID("")
|
||||
if err != nil {
|
||||
log.Fatalf("unable to generate machine id : %s", err)
|
||||
}
|
||||
|
@ -212,7 +227,7 @@ cscli machines add MyTestMachine --password MyPassword
|
|||
dumpFile = csConfig.API.Client.CredentialsFilePath
|
||||
}
|
||||
|
||||
// create password if doesn't specified by user
|
||||
// create a password if it's not specified by user
|
||||
if machinePassword == "" && !interactive {
|
||||
if !autoAdd {
|
||||
printHelp(cmd)
|
||||
|
|
|
@ -90,7 +90,7 @@ make_init_data() {
|
|||
./instance-db setup
|
||||
|
||||
"${CSCLI}" machines add githubciXXXXXXXXXXXXXXXXXXXXXXXX --auto
|
||||
"${CSCLI}" capi register
|
||||
"${CSCLI}" capi register --schmilblick githubciXXXXXXXXXXXXXXXXXXXXXXXX
|
||||
"${CSCLI}" hub update
|
||||
"${CSCLI}" collections install crowdsecurity/linux
|
||||
|
||||
|
|
Loading…
Reference in a new issue