|
| 1 | +// Copyright 2015 The Gogs Authors. All rights reserved. |
| 2 | +// Copyright 2017 The Gitea Authors. All rights reserved. |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +package git |
| 6 | + |
| 7 | +import ( |
| 8 | +"context" |
| 9 | +"fmt" |
| 10 | +"os" |
| 11 | +"strings" |
| 12 | +"sync" |
| 13 | + |
| 14 | +"code.gitea.io/gitea/modules/git/gitcmd" |
| 15 | +"code.gitea.io/gitea/modules/process" |
| 16 | +) |
| 17 | + |
| 18 | +// GPGSettings represents the default GPG settings for this repository |
| 19 | +type GPGSettings struct { |
| 20 | +Sign bool |
| 21 | +KeyID string |
| 22 | +Email string |
| 23 | +Name string |
| 24 | +PublicKeyContent string |
| 25 | +Format string |
| 26 | +} |
| 27 | + |
| 28 | +// LoadPublicKeyContent will load the key from gpg |
| 29 | +func (gpgSettings *GPGSettings) LoadPublicKeyContent() error { |
| 30 | +if gpgSettings.PublicKeyContent != "" { |
| 31 | +return nil |
| 32 | +} |
| 33 | + |
| 34 | +if gpgSettings.Format == SigningKeyFormatSSH { |
| 35 | +content, err := os.ReadFile(gpgSettings.KeyID) |
| 36 | +if err != nil { |
| 37 | +return fmt.Errorf("unable to read SSH public key file: %s, %w", gpgSettings.KeyID, err) |
| 38 | +} |
| 39 | +gpgSettings.PublicKeyContent = string(content) |
| 40 | +return nil |
| 41 | +} |
| 42 | +content, stderr, err := process.GetManager().Exec( |
| 43 | +"gpg -a --export", |
| 44 | +"gpg", "-a", "--export", gpgSettings.KeyID) |
| 45 | +if err != nil { |
| 46 | +return fmt.Errorf("unable to get default signing key: %s, %s, %w", gpgSettings.KeyID, stderr, err) |
| 47 | +} |
| 48 | +gpgSettings.PublicKeyContent = content |
| 49 | +return nil |
| 50 | +} |
| 51 | + |
| 52 | +var ( |
| 53 | +loadPublicGPGKeyMutex sync.RWMutex |
| 54 | +globalGPGSettings *GPGSettings |
| 55 | +) |
| 56 | + |
| 57 | +// GetDefaultPublicGPGKey will return and cache the default public GPG settings |
| 58 | +func GetDefaultPublicGPGKey(ctx context.Context, forceUpdate bool) (*GPGSettings, error) { |
| 59 | +if !forceUpdate { |
| 60 | +loadPublicGPGKeyMutex.RLock() |
| 61 | +if globalGPGSettings != nil { |
| 62 | +defer loadPublicGPGKeyMutex.RUnlock() |
| 63 | +return globalGPGSettings, nil |
| 64 | +} |
| 65 | +loadPublicGPGKeyMutex.RUnlock() |
| 66 | +} |
| 67 | + |
| 68 | +loadPublicGPGKeyMutex.Lock() |
| 69 | +defer loadPublicGPGKeyMutex.Unlock() |
| 70 | + |
| 71 | +if globalGPGSettings != nil && !forceUpdate { |
| 72 | +return globalGPGSettings, nil |
| 73 | +} |
| 74 | + |
| 75 | +globalGPGSettings = &GPGSettings{ |
| 76 | +Sign: true, |
| 77 | +} |
| 78 | + |
| 79 | +value, _, _ := gitcmd.NewCommand("config", "--global", "--get", "commit.gpgsign").RunStdString(ctx) |
| 80 | +sign, valid := ParseBool(strings.TrimSpace(value)) |
| 81 | +if !sign || !valid { |
| 82 | +globalGPGSettings.Sign = false |
| 83 | +return globalGPGSettings, nil |
| 84 | +} |
| 85 | + |
| 86 | +signingKey, _, _ := gitcmd.NewCommand("config", "--global", "--get", "user.signingkey").RunStdString(ctx) |
| 87 | +globalGPGSettings.KeyID = strings.TrimSpace(signingKey) |
| 88 | + |
| 89 | +format, _, _ := gitcmd.NewCommand("config", "--global", "--default", SigningKeyFormatOpenPGP, "--get", "gpg.format").RunStdString(ctx) |
| 90 | +globalGPGSettings.Format = strings.TrimSpace(format) |
| 91 | + |
| 92 | +defaultEmail, _, _ := gitcmd.NewCommand("config", "--global", "--get", "user.email").RunStdString(ctx) |
| 93 | +globalGPGSettings.Email = strings.TrimSpace(defaultEmail) |
| 94 | + |
| 95 | +defaultName, _, _ := gitcmd.NewCommand("config", "--global", "--get", "user.name").RunStdString(ctx) |
| 96 | +globalGPGSettings.Name = strings.TrimSpace(defaultName) |
| 97 | + |
| 98 | +if err := globalGPGSettings.LoadPublicKeyContent(); err != nil { |
| 99 | +return nil, err |
| 100 | +} |
| 101 | +return globalGPGSettings, nil |
| 102 | +} |
0 commit comments