Skip to content

Commit 5c56419

Browse files
lucarin91dido18
andauthored
feat(pkg/board): add SetPassword api (#588)
Co-authored-by: Davide <davideneri18@gmail.com>
1 parent 2c6afa5 commit 5c56419

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

cmd/arduino-app-cli/board/board.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package board
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"path"
78
"strconv"
89

910
"github.com/spf13/cobra"
11+
"golang.org/x/term"
1012

1113
"github.com/bcmi-labs/orchestrator/cmd/feedback"
1214
"github.com/bcmi-labs/orchestrator/pkg/appsync"
@@ -63,6 +65,7 @@ func NewBoardCmd() *cobra.Command {
6365
fsCmd.AddCommand(newSyncAppCmd())
6466
fsCmd.AddCommand(newBoardListCmd())
6567
fsCmd.AddCommand(newBoardSetName())
68+
fsCmd.AddCommand(newSetPasswordCmd())
6669

6770
return fsCmd
6871
}
@@ -161,3 +164,28 @@ func newBoardSetName() *cobra.Command {
161164

162165
return setNameCmd
163166
}
167+
168+
func newSetPasswordCmd() *cobra.Command {
169+
return &cobra.Command{
170+
Use: "set-password",
171+
Short: "Set the user password of the board",
172+
Args: cobra.ExactArgs(0),
173+
RunE: func(cmd *cobra.Command, args []string) error {
174+
conn := cmd.Context().Value(remoteConnKey).(remote.RemoteConn)
175+
176+
feedback.Print("Enter new password: ")
177+
// TODO: fix for not interactive terminal
178+
password, err := term.ReadPassword(int(os.Stdin.Fd())) // nolint:forbidigo
179+
if err != nil {
180+
return fmt.Errorf("failed to read password: %w", err)
181+
}
182+
183+
if err := board.SetUserPassword(cmd.Context(), conn, string(password)); err != nil {
184+
return fmt.Errorf("failed to set user password: %w", err)
185+
}
186+
187+
feedback.Printf("User password set\n")
188+
return nil
189+
},
190+
}
191+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
arduino ALL=(ALL) NOPASSWD: /usr/local/bin/arduino-passwd
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
read -s "pass"
4+
5+
[ -n "$pass" ] || exit 1
6+
7+
echo "arduino:$pass" | chpasswd
8+

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ require (
5050
go.bug.st/f v0.4.0
5151
go.bug.st/serial v1.6.4
5252
golang.org/x/crypto v0.40.0
53+
golang.org/x/term v0.34.0
5354
golang.org/x/text v0.28.0
5455
)
5556

@@ -272,7 +273,6 @@ require (
272273
golang.org/x/oauth2 v0.30.0 // indirect
273274
golang.org/x/sync v0.16.0 // indirect
274275
golang.org/x/sys v0.35.0 // indirect
275-
golang.org/x/term v0.34.0 // indirect
276276
golang.org/x/time v0.11.0 // indirect
277277
golang.org/x/tools v0.35.0 // indirect
278278
google.golang.org/genproto/googleapis/api v0.0.0-20250528174236-200df99c418a // indirect

pkg/board/board.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,29 @@ func IsUserPasswordSet(conn remote.RemoteShell) (bool, error) {
241241
return isUserSet, nil
242242
}
243243

244+
func SetUserPassword(ctx context.Context, conn remote.RemoteConn, newPass string) error {
245+
cmd := conn.GetCmd("sudo", "arduino-passwd")
246+
stdin, stdout, stderr, closer, err := cmd.Interactive()
247+
if err != nil {
248+
return fmt.Errorf("failed to set password: %w", err)
249+
}
250+
251+
if _, err = stdin.Write([]byte(newPass)); err != nil {
252+
return fmt.Errorf("failed to write password: %w", err)
253+
}
254+
if err := stdin.Close(); err != nil {
255+
return fmt.Errorf("failed to close stdin: %w", err)
256+
}
257+
258+
if err := closer(); err != nil {
259+
out, _ := io.ReadAll(stdout)
260+
errOut, _ := io.ReadAll(stderr)
261+
return fmt.Errorf("failed to set password: %w: %s %s", err, out, errOut)
262+
}
263+
264+
return nil
265+
}
266+
244267
func getSerial(conn remote.RemoteConn) (string, error) {
245268
f, err := conn.ReadFile(SerialPath)
246269
if err != nil {

0 commit comments

Comments
 (0)