Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "exec as" feature to public package interface #516

Merged
merged 6 commits into from
Dec 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion services/exec/client/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,27 @@ func ExecRemoteCommand(ctx context.Context, conn *proxy.Conn, binary string, arg
return result[0].Resp, nil
}

// ExecRemoteCommand is a helper function for execing a command on one or remote hosts
// ExecRemoteCommandAs is a helper function for execing a command on a remote host as specified user
// using a proxy.Conn. If the conn is defined for >1 targets this will return an error.
func ExecRemoteCommandAs(ctx context.Context, conn *proxy.Conn, user string, binary string, args ...string) (*pb.ExecResponse, error) {
sfc-gh-ikryvanos marked this conversation as resolved.
Show resolved Hide resolved
if len(conn.Targets) != 1 {
return nil, errors.New("ExecRemoteCommand only supports single targets")
}

result, err := ExecRemoteCommandManyAs(ctx, conn, user, binary, args...)
if err != nil {
return nil, err
}
if len(result) < 1 {
return nil, fmt.Errorf("ExecRemoteCommand error: received empty response")
}
if result[0].Error != nil {
return nil, fmt.Errorf("ExecRemoteCommand error: %v", result[0].Error)
}
return result[0].Resp, nil
}

// ExecRemoteCommandMany is a helper function for execing a command on one or remote hosts
// using a proxy.Conn.
// `binary` refers to the absolute path of the binary file on the remote host.
func ExecRemoteCommandMany(ctx context.Context, conn *proxy.Conn, binary string, args ...string) ([]*pb.RunManyResponse, error) {
sfc-gh-ikryvanos marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -65,3 +85,25 @@ func ExecRemoteCommandMany(ctx context.Context, conn *proxy.Conn, binary string,

return result, nil
}

// ExecRemoteCommandManyAs is a helper function for execing a command on one or remote hosts as specified user
// using a proxy.Conn.
// `binary` refers to the absolute path of the binary file on the remote host.
func ExecRemoteCommandManyAs(ctx context.Context, conn *proxy.Conn, user string, binary string, args ...string) ([]*pb.RunManyResponse, error) {
c := pb.NewExecClientProxy(conn)
req := &pb.ExecRequest{
User: user,
Command: binary,
Args: args,
}
respChan, err := c.RunOneMany(ctx, req)
if err != nil {
return nil, err
}
result := make([]*pb.RunManyResponse, len(conn.Targets))
for r := range respChan {
result[r.Index] = r
}

return result, nil
}
Loading