runSSHAuthContainer runs the SSH auth container using Docker client API
(keys []string)
| 227 | |
| 228 | // runSSHAuthContainer runs the SSH auth container using Docker client API |
| 229 | func runSSHAuthContainer(keys []string) (int, error) { |
| 230 | uid, _, _ := dockerutil.GetContainerUser() |
| 231 | // Run container as root to be able to change ownership of files |
| 232 | if dockerutil.IsDockerRootless() { |
| 233 | uid = "0" |
| 234 | } |
| 235 | |
| 236 | config := &container.Config{ |
| 237 | Image: docker.GetSSHAuthImage() + "-built", |
| 238 | Cmd: []string{"bash", "-c", GetAuthSSHCmd("ssh-add")}, |
| 239 | Entrypoint: []string{}, |
| 240 | AttachStdin: true, |
| 241 | User: uid, |
| 242 | } |
| 243 | |
| 244 | // Prepare mounts for Docker API |
| 245 | var mounts []mount.Mount |
| 246 | // Map to track already added keys |
| 247 | addedKeys := make(map[string]struct{}) |
| 248 | for i, keyPath := range keys { |
| 249 | filename := filepath.Base(keyPath) |
| 250 | // If it has the same name, change it to avoid conflicts |
| 251 | // This can happen if you have symlinks to the same key |
| 252 | if _, exists := addedKeys[filename]; exists { |
| 253 | filename = fmt.Sprintf("%s_%d", filename, i) |
| 254 | } |
| 255 | addedKeys[filename] = struct{}{} |
| 256 | mounts = append(mounts, mount.Mount{ |
| 257 | Type: mount.TypeBind, |
| 258 | Source: keyPath, |
| 259 | Target: "/tmp/sshtmp/" + filename, |
| 260 | ReadOnly: true, |
| 261 | }) |
| 262 | util.Debug("Binding SSH private key %s into container as /tmp/sshtmp/%s", keyPath, filename) |
| 263 | // Mount optional OpenSSH certificate |
| 264 | if certPath, certName := getCertificateForPrivateKey(keyPath, filename); certPath != "" && certName != "" { |
| 265 | mounts = append(mounts, mount.Mount{ |
| 266 | Type: mount.TypeBind, |
| 267 | Source: certPath, |
| 268 | Target: "/tmp/sshtmp/" + certName, |
| 269 | ReadOnly: true, |
| 270 | }) |
| 271 | util.Debug("Binding SSH certificate %s into container as /tmp/sshtmp/%s", certPath, certName) |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // Host configuration with volume mounts |
| 276 | hostConfig := &container.HostConfig{ |
| 277 | Mounts: mounts, |
| 278 | VolumesFrom: []string{ddevapp.SSHAuthName}, |
| 279 | } |
| 280 | |
| 281 | containerName := "ddev-ssh-auth-" + util.RandString(6) |
| 282 | _, _, err := dockerutil.RunSimpleContainerExtended(containerName, config, hostConfig, true, 60*time.Second) |
| 283 | |
| 284 | exitCode := 0 |
| 285 | if err != nil { |
| 286 | exitCode = 1 |
no test coverage detected