|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "os/exec" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/1Password/connect-sdk-go/onepassword" |
| 13 | + "github.com/Masterminds/semver/v3" |
| 14 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 15 | +) |
| 16 | + |
| 17 | +type OP struct { |
| 18 | + binaryPath string |
| 19 | + serviceAccountToken string |
| 20 | +} |
| 21 | + |
| 22 | +func New(serviceAccountToken, binaryPath string) *OP { |
| 23 | + return &OP{ |
| 24 | + binaryPath: binaryPath, |
| 25 | + serviceAccountToken: serviceAccountToken, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func (op *OP) GetVersion(ctx context.Context) (*semver.Version, error) { |
| 30 | + result, err := op.execRaw(ctx, nil, p("--version")) |
| 31 | + if err != nil { |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + versionString := strings.TrimSpace(string(result)) |
| 35 | + version, err := semver.NewVersion(versionString) |
| 36 | + if err != nil { |
| 37 | + return nil, fmt.Errorf("%w (input is: %s)", err, versionString) |
| 38 | + } |
| 39 | + return version, nil |
| 40 | +} |
| 41 | + |
| 42 | +func (op *OP) GetVault(ctx context.Context, uuid string) (*onepassword.Vault, error) { |
| 43 | + var res *onepassword.Vault |
| 44 | + err := op.execJson(ctx, &res, nil, p("vault"), p("get"), p(uuid)) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + return res, nil |
| 49 | +} |
| 50 | + |
| 51 | +func (op *OP) GetVaultsByTitle(ctx context.Context, title string) ([]onepassword.Vault, error) { |
| 52 | + var allVaults []onepassword.Vault |
| 53 | + err := op.execJson(ctx, &allVaults, nil, p("vault"), p("list")) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + var res []onepassword.Vault |
| 59 | + for _, v := range allVaults { |
| 60 | + if v.Name == title { |
| 61 | + res = append(res, v) |
| 62 | + } |
| 63 | + } |
| 64 | + return res, nil |
| 65 | +} |
| 66 | + |
| 67 | +func (op *OP) GetItem(ctx context.Context, itemUuid, vaultUuid string) (*onepassword.Item, error) { |
| 68 | + var res *onepassword.Item |
| 69 | + err := op.execJson(ctx, &res, nil, p("item"), p("get"), p(itemUuid), f("vault", vaultUuid)) |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + return res, nil |
| 74 | +} |
| 75 | + |
| 76 | +func (op *OP) GetItemByTitle(ctx context.Context, title string, vaultUuid string) (*onepassword.Item, error) { |
| 77 | + return op.GetItem(ctx, title, vaultUuid) |
| 78 | +} |
| 79 | + |
| 80 | +func (op *OP) CreateItem(ctx context.Context, item *onepassword.Item, vaultUuid string) (*onepassword.Item, error) { |
| 81 | + if item.Vault.ID != "" && item.Vault.ID != vaultUuid { |
| 82 | + return nil, errors.New("item payload contains vault id that does not match vault uuid") |
| 83 | + } |
| 84 | + item.Vault.ID = vaultUuid |
| 85 | + |
| 86 | + payload, err := json.Marshal(item) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + var res *onepassword.Item |
| 92 | + args := []opArg{p("item"), p("create"), p("-")} |
| 93 | + // 'op item create' command doesn't support generating passwords when using templates |
| 94 | + // therefore need to use --generate-password flag to set it |
| 95 | + if pf := passwordField(item); pf != nil { |
| 96 | + recipeStr := "letters,digits,32" |
| 97 | + if pf.Recipe != nil { |
| 98 | + recipeStr = passwordRecipeToString(pf.Recipe) |
| 99 | + } |
| 100 | + args = append(args, f("generate-password", recipeStr)) |
| 101 | + } |
| 102 | + |
| 103 | + err = op.execJson(ctx, &res, payload, args...) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + return res, nil |
| 108 | +} |
| 109 | + |
| 110 | +func (op *OP) UpdateItem(ctx context.Context, item *onepassword.Item, vaultUuid string) (*onepassword.Item, error) { |
| 111 | + if item.Vault.ID != "" && item.Vault.ID != vaultUuid { |
| 112 | + return nil, errors.New("item payload contains vault id that does not match vault uuid") |
| 113 | + } |
| 114 | + item.Vault.ID = vaultUuid |
| 115 | + |
| 116 | + payload, err := json.Marshal(item) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + |
| 121 | + var res *onepassword.Item |
| 122 | + err = op.execJson(ctx, &res, payload, p("item"), p("edit"), p(item.ID), f("vault", vaultUuid)) |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + return res, nil |
| 127 | +} |
| 128 | + |
| 129 | +func (op *OP) DeleteItem(ctx context.Context, item *onepassword.Item, vaultUuid string) error { |
| 130 | + if item.Vault.ID != "" && item.Vault.ID != vaultUuid { |
| 131 | + return errors.New("item payload contains vault id that does not match vault uuid") |
| 132 | + } |
| 133 | + item.Vault.ID = vaultUuid |
| 134 | + |
| 135 | + return op.execJson(ctx, nil, nil, p("item"), p("delete"), p(item.ID), f("vault", vaultUuid)) |
| 136 | +} |
| 137 | + |
| 138 | +func (op *OP) execJson(ctx context.Context, dst any, stdin []byte, args ...opArg) error { |
| 139 | + result, err := op.execRaw(ctx, stdin, args...) |
| 140 | + if err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + if dst != nil { |
| 144 | + return json.Unmarshal(result, dst) |
| 145 | + } |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func (op *OP) execRaw(ctx context.Context, stdin []byte, args ...opArg) ([]byte, error) { |
| 150 | + var cmdArgs []string |
| 151 | + for _, arg := range args { |
| 152 | + cmdArgs = append(cmdArgs, arg.format()) |
| 153 | + } |
| 154 | + |
| 155 | + cmd := exec.CommandContext(ctx, op.binaryPath, cmdArgs...) |
| 156 | + cmd.Env = append(cmd.Environ(), |
| 157 | + "OP_SERVICE_ACCOUNT_TOKEN="+op.serviceAccountToken, |
| 158 | + "OP_FORMAT=json", |
| 159 | + "OP_INTEGRATION_NAME=terraform-provider-connect", |
| 160 | + "OP_INTEGRATION_ID=GO", |
| 161 | + //"OP_INTEGRATION_BUILDNUMBER="+version.ProviderVersion, // causes bad request errors from CLI |
| 162 | + ) |
| 163 | + if stdin != nil { |
| 164 | + cmd.Stdin = bytes.NewReader(stdin) |
| 165 | + } |
| 166 | + |
| 167 | + tflog.Debug(ctx, "running op command: "+cmd.String()) |
| 168 | + |
| 169 | + result, err := cmd.Output() |
| 170 | + var exitError *exec.ExitError |
| 171 | + if errors.As(err, &exitError) { |
| 172 | + return nil, parseCliError(exitError.Stderr) |
| 173 | + } |
| 174 | + if err != nil { |
| 175 | + return nil, fmt.Errorf("failed to execute command: %w", err) |
| 176 | + } |
| 177 | + |
| 178 | + return result, nil |
| 179 | +} |
0 commit comments