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 CAPIURLPrefix string = "v2"
|
||||||
var CAPIBaseURL string = "https://api.crowdsec.net/"
|
var CAPIBaseURL string = "https://api.crowdsec.net/"
|
||||||
|
var capiUserPrefix string
|
||||||
|
|
||||||
func NewCapiCmd() *cobra.Command {
|
func NewCapiCmd() *cobra.Command {
|
||||||
var cmdCapi = &cobra.Command{
|
var cmdCapi = &cobra.Command{
|
||||||
|
@ -46,8 +47,7 @@ func NewCapiCmd() *cobra.Command {
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
var err error
|
var err error
|
||||||
|
capiUser, err := generateID(capiUserPrefix)
|
||||||
id, err := generateID()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to generate machine id: %s", err)
|
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)
|
log.Fatalf("unable to parse api url %s : %s", CAPIBaseURL, err)
|
||||||
}
|
}
|
||||||
_, err = apiclient.RegisterClient(&apiclient.Config{
|
_, err = apiclient.RegisterClient(&apiclient.Config{
|
||||||
MachineID: id,
|
MachineID: capiUser,
|
||||||
Password: password,
|
Password: password,
|
||||||
UserAgent: fmt.Sprintf("crowdsec/%s", cwversion.VersionStr()),
|
UserAgent: fmt.Sprintf("crowdsec/%s", cwversion.VersionStr()),
|
||||||
URL: apiurl,
|
URL: apiurl,
|
||||||
|
@ -79,7 +79,7 @@ func NewCapiCmd() *cobra.Command {
|
||||||
dumpFile = ""
|
dumpFile = ""
|
||||||
}
|
}
|
||||||
apiCfg := csconfig.ApiCredentialsCfg{
|
apiCfg := csconfig.ApiCredentialsCfg{
|
||||||
Login: id,
|
Login: capiUser,
|
||||||
Password: password.String(),
|
Password: password.String(),
|
||||||
URL: CAPIBaseURL,
|
URL: CAPIBaseURL,
|
||||||
}
|
}
|
||||||
|
@ -101,6 +101,8 @@ func NewCapiCmd() *cobra.Command {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cmdCapiRegister.Flags().StringVarP(&outputFile, "file", "f", "", "output file destination")
|
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)
|
cmdCapi.AddCommand(cmdCapiRegister)
|
||||||
|
|
||||||
var cmdCapiStatus = &cobra.Command{
|
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) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
var err error
|
var err error
|
||||||
if lapiUser == "" {
|
if lapiUser == "" {
|
||||||
lapiUser, err = generateID()
|
lapiUser, err = generateID("")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to generate machine id: %s", err)
|
log.Fatalf("unable to generate machine id: %s", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,21 +60,36 @@ func generatePassword(length int) string {
|
||||||
return string(buf)
|
return string(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateID() (string, error) {
|
// Returns a unique identifier for each crowdsec installation, using an
|
||||||
id, err := machineid.ID()
|
// 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
|
||||||
|
}
|
||||||
|
log.Debugf("failed to get machine-id with usual files: %s", err)
|
||||||
|
|
||||||
|
bID, err := ioutil.ReadFile(uuid)
|
||||||
|
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()
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("failed to get machine-id with usual files : %s", err)
|
return "", err
|
||||||
}
|
}
|
||||||
if id == "" || err != nil {
|
prefix = strings.ReplaceAll(prefix, "-", "")[:32]
|
||||||
bID, err := ioutil.ReadFile(uuid)
|
suffix := generatePassword(16)
|
||||||
if err != nil {
|
return prefix + suffix, nil
|
||||||
return "", errors.Wrap(err, "generating machine id")
|
|
||||||
}
|
|
||||||
id = string(bID)
|
|
||||||
}
|
|
||||||
id = strings.ReplaceAll(id, "-", "")[:32]
|
|
||||||
id = fmt.Sprintf("%s%s", id, generatePassword(16))
|
|
||||||
return id, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMachinesCmd() *cobra.Command {
|
func NewMachinesCmd() *cobra.Command {
|
||||||
|
@ -197,7 +212,7 @@ cscli machines add MyTestMachine --password MyPassword
|
||||||
printHelp(cmd)
|
printHelp(cmd)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
machineID, err = generateID()
|
machineID, err = generateID("")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to generate machine id : %s", err)
|
log.Fatalf("unable to generate machine id : %s", err)
|
||||||
}
|
}
|
||||||
|
@ -212,7 +227,7 @@ cscli machines add MyTestMachine --password MyPassword
|
||||||
dumpFile = csConfig.API.Client.CredentialsFilePath
|
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 machinePassword == "" && !interactive {
|
||||||
if !autoAdd {
|
if !autoAdd {
|
||||||
printHelp(cmd)
|
printHelp(cmd)
|
||||||
|
|
|
@ -90,7 +90,7 @@ make_init_data() {
|
||||||
./instance-db setup
|
./instance-db setup
|
||||||
|
|
||||||
"${CSCLI}" machines add githubciXXXXXXXXXXXXXXXXXXXXXXXX --auto
|
"${CSCLI}" machines add githubciXXXXXXXXXXXXXXXXXXXXXXXX --auto
|
||||||
"${CSCLI}" capi register
|
"${CSCLI}" capi register --schmilblick githubciXXXXXXXXXXXXXXXXXXXXXXXX
|
||||||
"${CSCLI}" hub update
|
"${CSCLI}" hub update
|
||||||
"${CSCLI}" collections install crowdsecurity/linux
|
"${CSCLI}" collections install crowdsecurity/linux
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue