Skip to content

Commit

Permalink
Enable identity validation for deprecated EngineBinary flow
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Weber <eric.weber@suse.com>
  • Loading branch information
ejweber committed Jul 24, 2023
1 parent ce99ddf commit ff51c6c
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 38 deletions.
9 changes: 5 additions & 4 deletions controller/engine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1610,10 +1610,11 @@ func GetBinaryClientForEngine(e *longhorn.Engine, engines engineapi.EngineClient
}

client, err = engines.NewEngineClient(&engineapi.EngineClientRequest{
VolumeName: e.Spec.VolumeName,
EngineImage: image,
IP: e.Status.IP,
Port: e.Status.Port,
VolumeName: e.Spec.VolumeName,
EngineImage: image,
IP: e.Status.IP,
Port: e.Status.Port,
InstanceName: e.Name,
})
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion engineapi/backup_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (m *BackupMonitor) syncBackupStatusFromEngineReplica() (currentBackupStatus
m.backupStatus.DeepCopyInto(&currentBackupStatus)
m.backupStatusLock.RUnlock()

engineBackupStatus, err = m.engineClientProxy.SnapshotBackupStatus(m.engine, m.backupName, m.replicaAddress)
engineBackupStatus, err = m.engineClientProxy.SnapshotBackupStatus(m.engine, m.backupName, m.replicaAddress, "")
if err != nil {
return currentBackupStatus, err
}
Expand Down
18 changes: 15 additions & 3 deletions engineapi/backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,10 @@ func (e *EngineBinary) SnapshotBackup(engine *longhorn.Engine, snapName, backupN
// TODO: update when replacing this function
snap, err := e.SnapshotGet(nil, snapName)
if err != nil {
return "", "", errors.Wrapf(err, "error getting snapshot '%s', volume '%s'", snapName, e.name)
return "", "", errors.Wrapf(err, "error getting snapshot '%s', volume '%s'", snapName, e.volumeName)
}
if snap == nil {
return "", "", errors.Errorf("could not find snapshot '%s' to backup, volume '%s'", snapName, e.name)
return "", "", errors.Errorf("could not find snapshot '%s' to backup, volume '%s'", snapName, e.volumeName)
}
version, err := e.VersionGet(nil, true)
if err != nil {
Expand Down Expand Up @@ -365,12 +365,24 @@ func (e *EngineBinary) SnapshotBackup(engine *longhorn.Engine, snapName, backupN

// SnapshotBackupStatus calls engine binary
// TODO: Deprecated, replaced by gRPC proxy
func (e *EngineBinary) SnapshotBackupStatus(engine *longhorn.Engine, backupName, replicaAddress string) (*longhorn.EngineBackupStatus, error) {
func (e *EngineBinary) SnapshotBackupStatus(engine *longhorn.Engine, backupName, replicaAddress,
replicaName string) (*longhorn.EngineBackupStatus, error) {
args := []string{"backup", "status", backupName}
if replicaAddress != "" {
args = append(args, "--replica", replicaAddress)
}

// For now, we likely don't know the replica name here. Don't bother checking the binary version if we don't.
if replicaName != "" {
version, err := e.VersionGet(engine, true)
if err != nil {
return nil, err
}
if version.ClientVersion.CLIAPIVersion >= 9 {
args = append(args, "--replica-instance-name", replicaName)
}
}

output, err := e.ExecuteEngineBinary(args...)
if err != nil {
return nil, err
Expand Down
64 changes: 47 additions & 17 deletions engineapi/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ const (
type EngineCollection struct{}

type EngineBinary struct {
name string
image string
ip string
port int
cURL string
volumeName string
image string
ip string
port int
cURL string
instanceName string
}

func (c *EngineCollection) NewEngineClient(request *EngineClientRequest) (*EngineBinary, error) {
Expand All @@ -46,34 +47,44 @@ func (c *EngineCollection) NewEngineClient(request *EngineClientRequest) (*Engin
}

return &EngineBinary{
name: request.VolumeName,
image: request.EngineImage,
ip: request.IP,
port: request.Port,
cURL: imutil.GetURL(request.IP, request.Port),
volumeName: request.VolumeName,
image: request.EngineImage,
ip: request.IP,
port: request.Port,
cURL: imutil.GetURL(request.IP, request.Port),
instanceName: request.InstanceName,
}, nil
}

func (e *EngineBinary) Name() string {
return e.name
return e.volumeName
}

func (e *EngineBinary) LonghornEngineBinary() string {
return filepath.Join(types.GetEngineBinaryDirectoryOnHostForImage(e.image), "longhorn")
}

func (e *EngineBinary) ExecuteEngineBinary(args ...string) (string, error) {
args = append([]string{"--url", e.cURL}, args...)
args, err := e.addFlags(args)
if err != nil {
return "", err
}
return util.Execute([]string{}, e.LonghornEngineBinary(), args...)
}

func (e *EngineBinary) ExecuteEngineBinaryWithTimeout(timeout time.Duration, args ...string) (string, error) {
args = append([]string{"--url", e.cURL}, args...)
args, err := e.addFlags(args)
if err != nil {
return "", err
}
return util.ExecuteWithTimeout(timeout, []string{}, e.LonghornEngineBinary(), args...)
}

func (e *EngineBinary) ExecuteEngineBinaryWithoutTimeout(envs []string, args ...string) (string, error) {
args = append([]string{"--url", e.cURL}, args...)
args, err := e.addFlags(args)
if err != nil {
return "", err
}
return util.ExecuteWithoutTimeout(envs, e.LonghornEngineBinary(), args...)
}

Expand All @@ -98,7 +109,7 @@ func parseReplica(s string) (*Replica, error) {
func (e *EngineBinary) ReplicaList(*longhorn.Engine) (map[string]*Replica, error) {
output, err := e.ExecuteEngineBinary("ls")
if err != nil {
return nil, errors.Wrapf(err, "failed to list replicas from controller '%s'", e.name)
return nil, errors.Wrapf(err, "failed to list replicas from controller '%s'", e.volumeName)
}
replicas := make(map[string]*Replica)
lines := strings.Split(output, "\n")
Expand Down Expand Up @@ -148,8 +159,12 @@ func (e *EngineBinary) ReplicaAdd(engine *longhorn.Engine, replicaName, url stri
}
}

if version.ClientVersion.CLIAPIVersion >= 9 {
cmd = append(cmd, "--replica-instance-name", replicaName)
}

if _, err := e.ExecuteEngineBinaryWithoutTimeout([]string{}, cmd...); err != nil {
return errors.Wrapf(err, "failed to add replica address='%s' to controller '%s'", url, e.name)
return errors.Wrapf(err, "failed to add replica address='%s' to controller '%s'", url, e.volumeName)
}
return nil
}
Expand All @@ -161,7 +176,7 @@ func (e *EngineBinary) ReplicaRemove(engine *longhorn.Engine, url string) error
return err
}
if _, err := e.ExecuteEngineBinary("rm", url); err != nil {
return errors.Wrapf(err, "failed to rm replica address='%s' from controller '%s'", url, e.name)
return errors.Wrapf(err, "failed to rm replica address='%s' from controller '%s'", url, e.volumeName)
}
return nil
}
Expand Down Expand Up @@ -304,3 +319,18 @@ func (e *EngineBinary) ReplicaModeUpdate(engine *longhorn.Engine, url, mode stri
func (e *EngineBinary) MetricsGet(*longhorn.Engine) (*Metrics, error) {
return nil, fmt.Errorf(ErrNotImplement)
}

// addFlags always adds required flags to args. In addition, if the engine version is high enough, it adds additional
// engine identity validation flags.
func (e *EngineBinary) addFlags(args []string) ([]string, error) {
version, err := e.VersionGet(nil, true)
if err != nil {
return args, errors.Wrap(err, "failed to get engine CLI version while adding identity flags")
}

argsToAdd := []string{"--url", e.cURL}
if version.ClientVersion.CLIAPIVersion >= 9 {
argsToAdd = append(argsToAdd, "--volume-name", e.volumeName, "--engine-instance-name", e.instanceName)
}
return append(argsToAdd, args...), nil
}
9 changes: 5 additions & 4 deletions engineapi/engine_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func GetEngineBinaryClient(ds *datastore.DataStore, volumeName, nodeID string) (

engineCollection := &EngineCollection{}
return engineCollection.NewEngineClient(&EngineClientRequest{
VolumeName: e.Spec.VolumeName,
EngineImage: e.Status.CurrentImage,
IP: e.Status.IP,
Port: e.Status.Port,
VolumeName: e.Spec.VolumeName,
EngineImage: e.Status.CurrentImage,
IP: e.Status.IP,
Port: e.Status.Port,
InstanceName: e.Name,
})
}
3 changes: 2 additions & 1 deletion engineapi/enginesim.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ func (e *EngineSimulator) SnapshotBackup(engine *longhorn.Engine, backupName, sn
return "", "", fmt.Errorf(ErrNotImplement)
}

func (e *EngineSimulator) SnapshotBackupStatus(engine *longhorn.Engine, backupName, replicaAddress string) (*longhorn.EngineBackupStatus, error) {
func (e *EngineSimulator) SnapshotBackupStatus(engine *longhorn.Engine, backupName, replicaAddress,
replicaName string) (*longhorn.EngineBackupStatus, error) {
return nil, fmt.Errorf(ErrNotImplement)
}

Expand Down
5 changes: 3 additions & 2 deletions engineapi/proxy_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ func (p *Proxy) SnapshotBackup(e *longhorn.Engine, snapshotName, backupName, bac
return backupID, replicaAddress, nil
}

func (p *Proxy) SnapshotBackupStatus(e *longhorn.Engine, backupName, replicaAddress string) (status *longhorn.EngineBackupStatus, err error) {
func (p *Proxy) SnapshotBackupStatus(e *longhorn.Engine, backupName, replicaAddress,
replicaName string) (status *longhorn.EngineBackupStatus, err error) {
recv, err := p.grpcClient.SnapshotBackupStatus(string(e.Spec.BackendStoreDriver), e.Name, e.Spec.VolumeName,
p.DirectToURL(e), backupName, replicaAddress, "")
p.DirectToURL(e), backupName, replicaAddress, replicaName)
if err != nil {
return nil, err
}
Expand Down
11 changes: 10 additions & 1 deletion engineapi/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,16 @@ func (e *EngineBinary) SnapshotPurgeStatus(*longhorn.Engine) (map[string]*longho
func (e *EngineBinary) SnapshotClone(engine *longhorn.Engine, snapshotName, fromEngineAddress, fromEngineName string,
fileSyncHTTPClientTimeout int64) error {
args := []string{"snapshot", "clone", "--snapshot-name", snapshotName, "--from-controller-address",
fromEngineAddress, "--from-controller-instance-name", fromEngineName}
fromEngineAddress}

version, err := e.VersionGet(engine, true)
if err != nil {
return err
}
if version.ClientVersion.CLIAPIVersion >= 9 {
args = append(args, "--from-controller-instance-name", fromEngineName)
}

if _, err := e.ExecuteEngineBinaryWithoutTimeout([]string{}, args...); err != nil {
return errors.Wrapf(err, "error starting snapshot clone")
}
Expand Down
11 changes: 6 additions & 5 deletions engineapi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type EngineClient interface {
SnapshotPurge(engine *longhorn.Engine) error
SnapshotPurgeStatus(engine *longhorn.Engine) (map[string]*longhorn.PurgeStatus, error)
SnapshotBackup(engine *longhorn.Engine, backupName, snapName, backupTarget, backingImageName, backingImageChecksum, compressionMethod string, concurrentLimit int, storageClassName string, labels, credential map[string]string) (string, string, error)
SnapshotBackupStatus(engine *longhorn.Engine, backupName, replicaAddress string) (*longhorn.EngineBackupStatus, error)
SnapshotBackupStatus(engine *longhorn.Engine, backupName, replicaAddress, replicaName string) (*longhorn.EngineBackupStatus, error)
SnapshotCloneStatus(engine *longhorn.Engine) (map[string]*longhorn.SnapshotCloneStatus, error)
SnapshotClone(engine *longhorn.Engine, snapshotName, fromEngineAddress, fromEngineName string, fileSyncHTTPClientTimeout int64) error
SnapshotHash(engine *longhorn.Engine, snapshotName string, rehash bool) error
Expand All @@ -107,10 +107,11 @@ type EngineClient interface {
}

type EngineClientRequest struct {
VolumeName string
EngineImage string
IP string
Port int
VolumeName string
EngineImage string
IP string
Port int
InstanceName string
}

type EngineClientCollection interface {
Expand Down

0 comments on commit ff51c6c

Please sign in to comment.