Skip to content

Commit fb734da

Browse files
committed
git-clone: Set up ssh keys
If ssh-directory exists, use it to set up ssh keys Signed-off-by: Tomáš Nevrlka <tnevrlka@redhat.com>
1 parent 6deaaf8 commit fb734da

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

pkg/commands/git_clone/git_clone.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ func (c *GitClone) Run() error {
7070
if err := c.setupBasicAuth(); err != nil {
7171
return err
7272
}
73+
if err := c.setupSSH(); err != nil {
74+
return err
75+
}
7376

7477
// Clean checkout directory if requested
7578
if c.Params.DeleteExisting {

pkg/commands/git_clone/setup.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,53 @@ func (c *GitClone) setupBasicAuth() error {
170170
return fmt.Errorf("unknown basic-auth workspace format: expected .git-credentials/.gitconfig or username/password files")
171171
}
172172

173+
// setupSSH sets up SSH keys from an ssh-directory workspace.
174+
func (c *GitClone) setupSSH() error {
175+
if c.Params.SshDirectory == "" {
176+
return nil
177+
}
178+
179+
sshDir := c.Params.SshDirectory
180+
userHome := c.Params.UserHome
181+
182+
// Check if the SSH directory exists
183+
if _, err := os.Stat(sshDir); os.IsNotExist(err) {
184+
l.Logger.Infof("SSH directory not found: %s", sshDir)
185+
return nil
186+
}
187+
188+
l.Logger.Infof("Setting up SSH keys from %s", sshDir)
189+
190+
destSSHDir := filepath.Join(userHome, ".ssh")
191+
192+
// Create destination .ssh directory
193+
if err := os.MkdirAll(destSSHDir, 0700); err != nil {
194+
return fmt.Errorf("failed to create .ssh directory: %w", err)
195+
}
196+
197+
// Copy all files from source to destination
198+
entries, err := os.ReadDir(sshDir)
199+
if err != nil {
200+
return fmt.Errorf("failed to read SSH directory: %w", err)
201+
}
202+
203+
for _, entry := range entries {
204+
if entry.IsDir() {
205+
continue // Skip subdirectories
206+
}
207+
208+
srcPath := filepath.Join(sshDir, entry.Name())
209+
destPath := filepath.Join(destSSHDir, entry.Name())
210+
211+
if err := copyFile(srcPath, destPath, 0400); err != nil {
212+
return fmt.Errorf("failed to copy SSH file %s: %w", entry.Name(), err)
213+
}
214+
}
215+
216+
l.Logger.Info("SSH keys configured")
217+
return nil
218+
}
219+
173220
// fileExists checks if a file exists and is not a directory.
174221
func fileExists(path string) bool {
175222
info, err := os.Stat(path)

0 commit comments

Comments
 (0)