forked from loft-sh/devpod
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathhelper.go
More file actions
190 lines (161 loc) · 4.54 KB
/
helper.go
File metadata and controls
190 lines (161 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package gitsshsigning
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/skevetter/devpod/pkg/command"
"github.com/skevetter/devpod/pkg/file"
"github.com/skevetter/log"
"github.com/skevetter/log/scanner"
)
const (
HelperScript = `#!/bin/bash
devpod agent git-ssh-signature "$@"
`
HelperScriptPath = "/usr/local/bin/devpod-ssh-signature"
GitConfigTemplate = `
[gpg "ssh"]
program = devpod-ssh-signature
[gpg]
format = ssh
[user]
signingkey = %s
`
)
// ConfigureHelper sets up the Git SSH signing helper script and updates the Git configuration for the specified user.
//
// This function:
// - sets user.signingkey git config
// - creates a wrapper script for calling git-ssh-signature
// - users this script as gpg.ssh.program
// This is needed since git expects `gpg.ssh.program` to be an executable.
func ConfigureHelper(userName, gitSigningKey string, log log.Logger) error {
log.Debug("Creating helper script")
if err := createHelperScript(); err != nil {
return err
}
log.Debugf("Helper script created. Making it executable.")
if err := makeScriptExecutable(); err != nil {
return err
}
log.Debugf("Script executable. Getting config path.")
gitConfigPath, err := getGitConfigPath(userName)
if err != nil {
return err
}
log.Debugf("Got config path: %v", gitConfigPath)
if err := updateGitConfig(gitConfigPath, userName, gitSigningKey); err != nil {
log.Errorf("Failed updating git configuration: %w", err)
return err
}
return nil
}
// RemoveHelper removes the git SSH signing helper script and any related configuration.
func RemoveHelper(userName string) error {
if err := os.Remove(HelperScriptPath); err != nil && !os.IsNotExist(err) {
return err
}
gitConfigPath, err := getGitConfigPath(userName)
if err != nil {
return err
}
if err := removeGitConfigHelper(gitConfigPath, userName); err != nil {
return err
}
return nil
}
func createHelperScript() error {
// we do it this way instead of os.Create because we need sudo
cmd := exec.Command(
"sudo",
"bash",
"-c",
fmt.Sprintf("echo '%s' > %s", HelperScript, HelperScriptPath),
)
if err := cmd.Run(); err != nil {
return err
}
return nil
}
func makeScriptExecutable() error {
return exec.Command("sudo", "chmod", "+x", HelperScriptPath).Run()
}
func getGitConfigPath(userName string) (string, error) {
homeDir, err := command.GetHome(userName)
if err != nil {
return "", err
}
return filepath.Join(homeDir, ".gitconfig"), nil
}
func updateGitConfig(gitConfigPath, userName, gitSigningKey string) error {
configContent, err := readGitConfig(gitConfigPath)
if err != nil {
return err
}
if !strings.Contains(configContent, "program = devpod-ssh-signature") {
newConfig := fmt.Sprintf(GitConfigTemplate, gitSigningKey)
newContent := removeSignatureHelper(configContent) + newConfig
if err := writeGitConfig(gitConfigPath, newContent, userName); err != nil {
return err
}
}
return nil
}
func readGitConfig(gitConfigPath string) (string, error) {
out, err := os.ReadFile(gitConfigPath)
if err != nil && !os.IsNotExist(err) {
return "", err
}
return string(out), nil
}
func writeGitConfig(gitConfigPath, content, userName string) error {
if err := os.WriteFile(gitConfigPath, []byte(content), 0o600); err != nil {
return fmt.Errorf("write git config: %w", err)
}
return file.Chown(userName, gitConfigPath)
}
func removeGitConfigHelper(gitConfigPath, userName string) error {
configContent, err := readGitConfig(gitConfigPath)
if err != nil {
return err
}
newContent := removeSignatureHelper(configContent)
if err := writeGitConfig(gitConfigPath, newContent, userName); err != nil {
return err
}
return nil
}
func removeSignatureHelper(content string) string {
scan := scanner.NewScanner(strings.NewReader(content))
inGpgSSHSection := false
inGpgSection := false
out := []string{}
for scan.Scan() {
line := scan.Text()
trimmed := strings.TrimSpace(line)
// Track section transitions
if len(trimmed) > 0 && trimmed[0] == '[' {
inGpgSSHSection = trimmed == `[gpg "ssh"]`
inGpgSection = trimmed == "[gpg]"
// Skip the entire [gpg "ssh"] section header (devpod-managed)
if inGpgSSHSection {
continue
}
}
// Skip all lines inside [gpg "ssh"] section
if inGpgSSHSection {
continue
}
// Inside [gpg] section, only skip devpod-managed keys
if inGpgSection && len(trimmed) > 0 && trimmed[0] != '[' {
if strings.HasPrefix(trimmed, "format = ssh") ||
strings.HasPrefix(trimmed, "program = devpod-ssh-signature") {
continue
}
}
out = append(out, line)
}
return strings.Join(out, "\n")
}