|
| 1 | +package secretfile |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "filippo.io/age" |
| 8 | + "filippo.io/age/agessh" |
| 9 | +) |
| 10 | + |
| 11 | +type ( |
| 12 | + // PromptFunc is a callback invoked by the store when encrypting or |
| 13 | + // decrypting a file. The function is expected to return the key material |
| 14 | + // (as a byte slice) or an error if the key cannot be obtained. |
| 15 | + PromptFunc func(context.Context) ([]byte, error) |
| 16 | + |
| 17 | + // KeyType identifies the type of encryption or decryption key associated |
| 18 | + // with a secret (e.g., password, age, or SSH). |
| 19 | + KeyType string |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + PasswordKeyType KeyType = "pass" |
| 24 | + AgeKeyType KeyType = "age" |
| 25 | + SSHKeyType KeyType = "ssh" |
| 26 | +) |
| 27 | + |
| 28 | +func getRecipient(k KeyType, encryptionKey string) (age.Recipient, error) { |
| 29 | + var recipient age.Recipient |
| 30 | + var err error |
| 31 | + |
| 32 | + switch k { |
| 33 | + case PasswordKeyType: |
| 34 | + recipient, err = age.NewScryptRecipient(encryptionKey) |
| 35 | + case AgeKeyType: |
| 36 | + recipient, err = age.ParseX25519Recipient(encryptionKey) |
| 37 | + case SSHKeyType: |
| 38 | + recipient, err = agessh.ParseRecipient(encryptionKey) |
| 39 | + default: |
| 40 | + return nil, fmt.Errorf("unsupported encryption type %T", k) |
| 41 | + } |
| 42 | + |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + |
| 47 | + return recipient, nil |
| 48 | +} |
| 49 | + |
| 50 | +// GetRecipients returns a slice of [age.Recipient] for the given key type and |
| 51 | +// encryption keys. |
| 52 | +// |
| 53 | +// The recipient implementation depends on the provided [KeyType]: |
| 54 | +// - passwordKeyType → [age.NewScryptRecipient] |
| 55 | +// - ageKeyType → [age.ParseX25519Recipient] |
| 56 | +// - sshKeyType → [agessh.ParseRecipient] |
| 57 | +// |
| 58 | +// An error is returned if the key cannot be parsed or the key type is |
| 59 | +// unsupported. |
| 60 | +func GetRecipients(k KeyType, encryptionKeys []string) ([]age.Recipient, error) { |
| 61 | + var recipients []age.Recipient |
| 62 | + for _, encryptionKey := range encryptionKeys { |
| 63 | + recipient, err := getRecipient(k, encryptionKey) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + recipients = append(recipients, recipient) |
| 68 | + } |
| 69 | + return recipients, nil |
| 70 | +} |
| 71 | + |
| 72 | +// GetIdentity returns an [age.Identity] for the given key type and |
| 73 | +// decryption key. |
| 74 | +// |
| 75 | +// The identity implementation depends on the provided [KeyType]: |
| 76 | +// - PasswordKeyType → [age.NewScryptIdentity] |
| 77 | +// - AgeKeyType → [age.ParseX25519Identity] |
| 78 | +// - SSHKeyType → [agessh.ParseIdentity] |
| 79 | +// |
| 80 | +// An error is returned if the key cannot be parsed or the key type is |
| 81 | +// unsupported. |
| 82 | +func GetIdentity(k KeyType, decryptionKey string) (age.Identity, error) { |
| 83 | + var identity age.Identity |
| 84 | + var err error |
| 85 | + |
| 86 | + switch k { |
| 87 | + case PasswordKeyType: |
| 88 | + identity, err = age.NewScryptIdentity(decryptionKey) |
| 89 | + case AgeKeyType: |
| 90 | + identity, err = age.ParseX25519Identity(decryptionKey) |
| 91 | + case SSHKeyType: |
| 92 | + identity, err = agessh.ParseIdentity([]byte(decryptionKey)) |
| 93 | + default: |
| 94 | + return nil, fmt.Errorf("unsupported decryption type %T", k) |
| 95 | + } |
| 96 | + |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + return identity, nil |
| 102 | +} |
0 commit comments