Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add bitwardenSecrets template function
- Loading branch information
Showing
10 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
assets/chezmoi.io/docs/reference/templates/bitwarden-functions/bitwardenSecrets.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# `bitwardenSecrets` *secret-id* [*access-token*] | ||
|
||
`bitwardenSecrets` returns structured data from | ||
[Bitwarden](https://bitwarden.com) using the [Bitwarden Secrets | ||
CLI](https://bitwarden.com/help/secrets-manager-cli/) (`bws`). *secret-id* is | ||
passed to `bws secret get` and the output from `bws secret get` is parsed as | ||
JSON and returned. | ||
|
||
If the additional *access-token* argument is given, it is passed to `bws secret | ||
get` with the `--access-token` flag. | ||
|
||
The output from `bws secret get` is cached so calling `bitwardenSecrets` | ||
multiple times with the same *secret-id* and *access-token* will only invoke | ||
`bws secret get ` once. | ||
|
||
!!! | ||
|
||
``` | ||
{{ (bitwardenSecrets "be8e0ad8-d545-4017-a55a-b02f014d4158").value }} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/twpayne/chezmoi/v2/internal/chezmoilog" | ||
) | ||
|
||
type bitwardenSecretsConfig struct { | ||
Command string `json:"command" mapstructure:"command" yaml:"command"` | ||
outputCache map[string][]byte | ||
} | ||
|
||
func (c *Config) bitwardenSecretsTemplateFunc(secretID string, additionalArgs ...string) any { | ||
args := []string{"secret", "get", secretID} | ||
switch len(additionalArgs) { | ||
case 0: | ||
// Do nothing. | ||
case 1: | ||
args = append(args, "--access-token", additionalArgs[0]) | ||
default: | ||
panic(fmt.Errorf("expected 1 or 2 arguments, got %d", len(additionalArgs)+1)) | ||
} | ||
output, err := c.bitwardenSecretsOutput(args) | ||
if err != nil { | ||
panic(err) | ||
} | ||
var data map[string]any | ||
if err := json.Unmarshal(output, &data); err != nil { | ||
panic(newParseCmdOutputError(c.BitwardenSecrets.Command, args, output, err)) | ||
} | ||
return data | ||
} | ||
|
||
func (c *Config) bitwardenSecretsOutput(args []string) ([]byte, error) { | ||
key := strings.Join(args, "\x00") | ||
if data, ok := c.Bitwarden.outputCache[key]; ok { | ||
return data, nil | ||
} | ||
|
||
name := c.BitwardenSecrets.Command | ||
cmd := exec.Command(name, args...) | ||
cmd.Stdin = os.Stdin | ||
cmd.Stderr = os.Stderr | ||
output, err := chezmoilog.LogCmdOutput(cmd) | ||
if err != nil { | ||
return nil, newCmdOutputError(cmd, output, err) | ||
} | ||
|
||
if c.BitwardenSecrets.outputCache == nil { | ||
c.BitwardenSecrets.outputCache = make(map[string][]byte) | ||
} | ||
c.BitwardenSecrets.outputCache[key] = output | ||
return output, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters