2019-06-21 01:32:00 +00:00
package command
import (
"fmt"
"strings"
"github.com/hashicorp/vault/api"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
var _ cli . Command = ( * OperatorRaftJoinCommand ) ( nil )
var _ cli . CommandAutocomplete = ( * OperatorRaftJoinCommand ) ( nil )
type OperatorRaftJoinCommand struct {
2019-10-11 18:56:59 +00:00
flagRetry bool
2019-06-21 21:41:07 +00:00
flagLeaderCACert string
flagLeaderClientCert string
flagLeaderClientKey string
2019-10-11 18:56:59 +00:00
flagNonVoter bool
2019-06-21 01:32:00 +00:00
* BaseCommand
}
func ( c * OperatorRaftJoinCommand ) Synopsis ( ) string {
2020-03-17 13:46:44 +00:00
return "Joins a node to the Raft cluster"
2019-06-21 01:32:00 +00:00
}
func ( c * OperatorRaftJoinCommand ) Help ( ) string {
helpText := `
Usage : vault operator raft join [ options ] < leader - api - addr >
2020-03-17 13:46:44 +00:00
Join the current node as a peer to the Raft cluster by providing the address
of the Raft leader node .
2019-06-21 01:32:00 +00:00
2020-05-23 15:09:55 +00:00
$ vault operator raft join "http://127.0.0.2:8200"
TLS certificate data can also be consumed from a file on disk by prefixing with
the "@" symbol . For example :
$ vault operator raft join "http://127.0.0.2:8200" \
- leader - ca - cert = @ leader_ca . crt \
- leader - client - cert = @ leader_client . crt \
- leader - client - key = @ leader . key
2019-06-21 01:32:00 +00:00
` + c . Flags ( ) . Help ( )
return strings . TrimSpace ( helpText )
}
func ( c * OperatorRaftJoinCommand ) Flags ( ) * FlagSets {
set := c . flagSet ( FlagSetHTTP | FlagSetOutputFormat )
f := set . NewFlagSet ( "Command Options" )
f . StringVar ( & StringVar {
2019-06-21 21:41:07 +00:00
Name : "leader-ca-cert" ,
Target : & c . flagLeaderCACert ,
2019-06-21 01:32:00 +00:00
Completion : complete . PredictNothing ,
2020-06-02 14:17:08 +00:00
Usage : "CA cert to use when verifying the Raft leader certificate." ,
2019-06-21 01:32:00 +00:00
} )
2019-06-21 21:41:07 +00:00
f . StringVar ( & StringVar {
Name : "leader-client-cert" ,
Target : & c . flagLeaderClientCert ,
Completion : complete . PredictNothing ,
2020-06-02 14:17:08 +00:00
Usage : "Client cert to use when authenticating with the Raft leader." ,
2019-06-21 21:41:07 +00:00
} )
f . StringVar ( & StringVar {
Name : "leader-client-key" ,
Target : & c . flagLeaderClientKey ,
Completion : complete . PredictNothing ,
2020-06-02 14:17:08 +00:00
Usage : "Client key to use when authenticating with the Raft leader." ,
2019-06-21 21:41:07 +00:00
} )
2019-06-21 01:32:00 +00:00
f . BoolVar ( & BoolVar {
Name : "retry" ,
2019-10-11 18:56:59 +00:00
Target : & c . flagRetry ,
2019-06-21 01:32:00 +00:00
Default : false ,
2020-03-17 13:46:44 +00:00
Usage : "Continuously retry joining the Raft cluster upon failures." ,
2019-06-21 01:32:00 +00:00
} )
2019-10-11 18:56:59 +00:00
f . BoolVar ( & BoolVar {
Name : "non-voter" ,
Target : & c . flagNonVoter ,
Default : false ,
Usage : "(Enterprise-only) This flag is used to make the server not participate in the Raft quorum, and have it only receive the data replication stream. This can be used to add read scalability to a cluster in cases where a high volume of reads to servers are needed." ,
} )
2019-06-21 01:32:00 +00:00
return set
}
func ( c * OperatorRaftJoinCommand ) AutocompleteArgs ( ) complete . Predictor {
return complete . PredictAnything
}
func ( c * OperatorRaftJoinCommand ) AutocompleteFlags ( ) complete . Flags {
return c . Flags ( ) . Completions ( )
}
func ( c * OperatorRaftJoinCommand ) Run ( args [ ] string ) int {
f := c . Flags ( )
if err := f . Parse ( args ) ; err != nil {
c . UI . Error ( err . Error ( ) )
return 1
}
leaderAPIAddr := ""
args = f . Args ( )
switch len ( args ) {
2020-06-23 19:04:13 +00:00
case 0 :
// No-op: This is acceptable if we're using raft for HA-only
2019-06-21 01:32:00 +00:00
case 1 :
leaderAPIAddr = strings . TrimSpace ( args [ 0 ] )
default :
2020-06-23 19:04:13 +00:00
c . UI . Error ( fmt . Sprintf ( "Too many arguments (expected 0-1, got %d)" , len ( args ) ) )
2019-06-21 01:32:00 +00:00
return 1
}
2020-05-23 15:09:55 +00:00
leaderCACert , err := parseFlagFile ( c . flagLeaderCACert )
if err != nil {
c . UI . Error ( fmt . Sprintf ( "Failed to parse leader CA certificate: %s" , err ) )
return 1
}
leaderClientCert , err := parseFlagFile ( c . flagLeaderClientCert )
if err != nil {
c . UI . Error ( fmt . Sprintf ( "Failed to parse leader client certificate: %s" , err ) )
return 1
}
leaderClientKey , err := parseFlagFile ( c . flagLeaderClientKey )
if err != nil {
c . UI . Error ( fmt . Sprintf ( "Failed to parse leader client key: %s" , err ) )
return 1
}
2019-06-21 01:32:00 +00:00
client , err := c . Client ( )
if err != nil {
c . UI . Error ( err . Error ( ) )
return 2
}
resp , err := client . Sys ( ) . RaftJoin ( & api . RaftJoinRequest {
2019-06-21 21:41:07 +00:00
LeaderAPIAddr : leaderAPIAddr ,
2020-05-23 15:09:55 +00:00
LeaderCACert : leaderCACert ,
LeaderClientCert : leaderClientCert ,
LeaderClientKey : leaderClientKey ,
2019-10-11 18:56:59 +00:00
Retry : c . flagRetry ,
NonVoter : c . flagNonVoter ,
2019-06-21 01:32:00 +00:00
} )
if err != nil {
2020-03-17 13:46:44 +00:00
c . UI . Error ( fmt . Sprintf ( "Error joining the node to the Raft cluster: %s" , err ) )
2019-06-21 01:32:00 +00:00
return 2
}
switch Format ( c . UI ) {
case "table" :
default :
return OutputData ( c . UI , resp )
}
2020-05-23 15:09:55 +00:00
out := [ ] string {
"Key | Value" ,
fmt . Sprintf ( "Joined | %t" , resp . Joined ) ,
}
2019-06-21 01:32:00 +00:00
c . UI . Output ( tableOutput ( out , nil ) )
return 0
}