MCPcopy Create free account
hub / github.com/aptly-dev/aptly / apiGPGAddKey

Function apiGPGAddKey

api/gpg.go:70–136  ·  view source on GitHub ↗

@Summary Add GPG Keys @Description **Adds GPG keys to aptly keyring** @Description @Description Add GPG public keys for verifying remote repositories for mirroring. @Description @Description Keys can be added in two ways: @Description * By providing the ASCII armord key in `GpgKeyArmor` (leave Keyse

(c *gin.Context)

Source from the content-addressed store, hash-verified

68// @Failure 400 {object} Error "Bad Request"
69// @Router /api/gpg/key [post]
70func apiGPGAddKey(c *gin.Context) {
71 b := gpgAddKeyParams{}
72 if c.Bind(&b) != nil {
73 return
74 }
75 b.Keyserver = utils.SanitizePath(b.Keyserver)
76 b.GpgKeyID = utils.SanitizePath(b.GpgKeyID)
77 b.GpgKeyArmor = utils.SanitizePath(b.GpgKeyArmor)
78 // b.Keyring can be an absolute path
79
80 var err error
81 args := []string{"--no-default-keyring", "--allow-non-selfsigned-uid"}
82 keyring := "trustedkeys.gpg"
83 if len(b.Keyring) > 0 {
84 keyring = b.Keyring
85 }
86 args = append(args, "--keyring", keyring)
87 if len(b.Keyserver) > 0 {
88 args = append(args, "--keyserver", b.Keyserver)
89 }
90 if len(b.GpgKeyArmor) > 0 {
91 var tempdir string
92 tempdir, err = os.MkdirTemp(os.TempDir(), "aptly")
93 if err != nil {
94 AbortWithJSONError(c, 400, err)
95 return
96 }
97 defer func() { _ = os.RemoveAll(tempdir) }()
98
99 keypath := filepath.Join(tempdir, "key")
100 keyfile, e := os.Create(keypath)
101 if e != nil {
102 AbortWithJSONError(c, 400, e)
103 return
104 }
105 if _, e = keyfile.WriteString(b.GpgKeyArmor); e != nil {
106 AbortWithJSONError(c, 400, e)
107 }
108 args = append(args, "--import", keypath)
109
110 }
111 if len(b.GpgKeyID) > 0 {
112 keys := strings.Fields(b.GpgKeyID)
113 args = append(args, "--recv-keys")
114 args = append(args, keys...)
115 }
116
117 finder := pgp.GPGDefaultFinder()
118 gpg, _, err := finder.FindGPG()
119 if err != nil {
120 AbortWithJSONError(c, 400, err)
121 return
122 }
123
124 // it might happened that we have a situation with an erroneous
125 // gpg command (e.g. when GpgKeyID and GpgKeyArmor is set).
126 // there is no error handling for such as gpg will do this for us
127 cmd := exec.Command(gpg, args...)

Callers

nothing calls this directly

Calls 6

SanitizePathFunction · 0.92
GPGDefaultFinderFunction · 0.92
AbortWithJSONErrorFunction · 0.85
WriteStringMethod · 0.80
FindGPGMethod · 0.65
PrintfMethod · 0.65

Tested by

no test coverage detected