@@ -8,11 +8,13 @@ import (
88 "errors"
99 "fmt"
1010 "io"
11+ "os"
1112
12- "github.com/sirupsen/logrus"
1313 "github.com/spf13/cobra"
1414
15+ drv "github.com/lima-vm/lima/v2/pkg/driver/vz"
1516 "github.com/lima-vm/lima/v2/pkg/networks"
17+ pkgsudoers "github.com/lima-vm/lima/v2/pkg/sudoers"
1618)
1719
1820func sudoersAction (cmd * cobra.Command , args []string ) error {
@@ -21,11 +23,6 @@ func sudoersAction(cmd *cobra.Command, args []string) error {
2123 if err != nil {
2224 return err
2325 }
24- // Make sure the current network configuration is secure
25- if err := nwCfg .Validate (); err != nil {
26- logrus .Infof ("Please check %s for more information." , socketVMNetURL )
27- return err
28- }
2926 check , err := cmd .Flags ().GetBool ("check" )
3027 if err != nil {
3128 return err
@@ -41,10 +38,15 @@ func sudoersAction(cmd *cobra.Command, args []string) error {
4138 default :
4239 return fmt .Errorf ("unexpected arguments %v" , args )
4340 }
44- sudoers , err := networks .Sudoers ()
41+ networkSudoers , err := networks .Sudoers ()
42+ if err != nil {
43+ return err
44+ }
45+ blockDeviceSudoers , err := drv .BlockDeviceSudoers (nwCfg .Group )
4546 if err != nil {
4647 return err
4748 }
49+ sudoers := pkgsudoers .AssembleSudoersFragments (networkSudoers , blockDeviceSudoers )
4850 fmt .Fprint (cmd .OutOrStdout (), sudoers )
4951 return nil
5052}
@@ -63,9 +65,38 @@ func verifySudoAccess(ctx context.Context, nwCfg networks.Config, args []string,
6365 default :
6466 return errors .New ("can check only a single sudoers file" )
6567 }
66- if err := nwCfg . VerifySudoAccess (ctx , file ); err != nil {
68+ if err := verifySudoersFile (ctx , nwCfg , file ); err != nil {
6769 return err
6870 }
6971 fmt .Fprintf (stdout , "%q is up-to-date (or sudo doesn't require a password)\n " , file )
7072 return nil
7173}
74+
75+ func verifySudoersFile (ctx context.Context , nwCfg networks.Config , file string ) error {
76+ hint := fmt .Sprintf ("run `%s sudoers >etc_sudoers.d_lima && sudo install -o root etc_sudoers.d_lima %q`)" ,
77+ os .Args [0 ], file )
78+ b , err := os .ReadFile (file )
79+ if err != nil {
80+ if errors .Is (err , os .ErrNotExist ) {
81+ if err := nwCfg .VerifySudoAccess (ctx , "" ); err == nil {
82+ if err := pkgsudoers .Run (ctx , "root" , "wheel" , nil , nil , nil , "" , "true" ); err == nil {
83+ return nil
84+ }
85+ }
86+ }
87+ return fmt .Errorf ("can't read %q: %w: (Hint: %s)" , file , err , hint )
88+ }
89+ networkSudoers , err := networks .Sudoers ()
90+ if err != nil {
91+ return err
92+ }
93+ blockDeviceSudoers , err := drv .BlockDeviceSudoers (nwCfg .Group )
94+ if err != nil {
95+ return err
96+ }
97+ sudoers := pkgsudoers .AssembleSudoersFragments (networkSudoers , blockDeviceSudoers )
98+ if string (b ) != sudoers {
99+ return fmt .Errorf ("sudoers file %q is out of sync and must be regenerated (Hint: %s)" , file , hint )
100+ }
101+ return nil
102+ }
0 commit comments