@@ -102,23 +102,33 @@ var receiveCmd = &cobra.Command{
102102 instanceName = strings .TrimSuffix (fileName , filepath .Ext (fileName ))
103103 }
104104
105- targetPath := filepath .Join (sshDir , fileName )
106-
107- // 4. Save file with secure permissions
108- // Open destination file with 0600 permissions (read/write owner)
109- err = os .WriteFile (targetPath , fileContent , 0600 )
110- if err != nil {
111- log .Fatalf ("Failed to write key file: %v" , err )
105+ targetPath := filepath .Join (sshDir , fileName )
106+
107+ // 4. Remove existing file if it exists (it might be read-only)
108+ if _ , err := os .Stat (targetPath ); err == nil {
109+ // File exists, make it writable first then remove it
110+ os .Chmod (targetPath , 0600 )
111+ if err := os .Remove (targetPath ); err != nil {
112+ log .Fatalf ("Failed to remove existing key file: %v" , err )
112113 }
114+ fmt .Printf ("Removed existing key file: %s\n " , targetPath )
115+ }
113116
114- // Now lock it down to read-only (0400)
115- if err := os .Chmod (targetPath , 0400 ); err != nil {
116- log .Printf ("Warning: failed to set secure permissions (0400): %v" , err )
117- }
117+ // 5. Save file with secure permissions
118+ // Open destination file with 0600 permissions (read/write owner)
119+ err = os .WriteFile (targetPath , fileContent , 0600 )
120+ if err != nil {
121+ log .Fatalf ("Failed to write key file: %v" , err )
122+ }
123+
124+ // Now lock it down to read-only (0400)
125+ if err := os .Chmod (targetPath , 0400 ); err != nil {
126+ log .Printf ("Warning: failed to set secure permissions (0400): %v" , err )
127+ }
118128
119129 fmt .Printf ("Successfully received key: %s\n " , targetPath )
120130
121- // 5 . Save to database
131+ // 6 . Save to database
122132 fmt .Println ("Adding key to gotoni database..." )
123133 database , err := db .InitDB ()
124134 if err != nil {
@@ -131,7 +141,7 @@ var receiveCmd = &cobra.Command{
131141 }
132142 }
133143
134- // 6 . Update SSH config (~/.ssh/config)
144+ // 7 . Update SSH config (~/.ssh/config)
135145 if instanceIP != "" {
136146 fmt .Printf ("Configuring SSH access for %s (%s)...\n " , instanceName , instanceIP )
137147 if err := client .UpdateSSHConfig (instanceName , instanceIP , targetPath ); err != nil {
0 commit comments