Skip to content

Commit

Permalink
feat(#222): manage v5 sensitive values
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuochi committed Oct 14, 2023
1 parent 78dc482 commit a799aa4
Show file tree
Hide file tree
Showing 42 changed files with 312 additions and 117 deletions.
4 changes: 2 additions & 2 deletions docs/resources/download_client.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ resource "radarr_download_client" "example" {
- `add_paused` (Boolean) Add paused flag.
- `add_stopped` (Boolean) Add stopped flag.
- `additional_tags` (Set of Number) Additional tags, `0` TitleSlug, `1` Quality, `2` Language, `3` ReleaseGroup, `4` Year, `5` Indexer, `6` Network.
- `api_key` (String) API key.
- `api_key` (String, Sensitive) API key.
- `api_url` (String) API URL.
- `app_id` (String) App ID.
- `app_token` (String, Sensitive) App Token.
Expand Down Expand Up @@ -74,7 +74,7 @@ resource "radarr_download_client" "example" {
- `remove_failed_downloads` (Boolean) Remove failed downloads flag.
- `rpc_path` (String) RPC path.
- `save_magnet_files` (Boolean) Save magnet files flag.
- `secret_token` (String) Secret token.
- `secret_token` (String, Sensitive) Secret token.
- `sequential_order` (Boolean) Sequential order flag.
- `start_on_add` (Boolean) Start on add flag.
- `strm_folder` (String) STRM folder.
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/download_client_aria2.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ resource "radarr_download_client_aria2" "example" {
- `remove_completed_downloads` (Boolean) Remove completed downloads flag.
- `remove_failed_downloads` (Boolean) Remove failed downloads flag.
- `rpc_path` (String) RPC path.
- `secret_token` (String) Secret token.
- `secret_token` (String, Sensitive) Secret token.
- `tags` (Set of Number) List of associated tags.
- `use_ssl` (Boolean) Use SSL flag.

Expand Down
2 changes: 1 addition & 1 deletion docs/resources/download_client_nzbvortex.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ resource "radarr_download_client_nzbvortex" "example" {

### Required

- `api_key` (String) API key.
- `api_key` (String, Sensitive) API key.
- `name` (String) Download Client name.

### Optional
Expand Down
4 changes: 2 additions & 2 deletions docs/resources/indexer.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ resource "radarr_indexer" "example" {

- `additional_parameters` (String) Additional parameters.
- `allow_zero_size` (Boolean) Allow zero size files.
- `api_key` (String) API key.
- `api_key` (String, Sensitive) API key.
- `api_path` (String) API path.
- `api_user` (String) API User.
- `base_url` (String) Base URL.
Expand All @@ -58,7 +58,7 @@ resource "radarr_indexer" "example" {
- `mediums` (Set of Number) Mediumd.
- `minimum_seeders` (Number) Minimum seeders.
- `multi_languages` (Set of Number) Language list.
- `passkey` (String) Passkey.
- `passkey` (String, Sensitive) Passkey.
- `priority` (Number) Priority.
- `ranked_only` (Boolean) Allow ranked only.
- `remove_year` (Boolean) Remove year.
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/indexer_newznab.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ resource "radarr_indexer_newznab" "example" {
### Optional

- `additional_parameters` (String) Additional parameters.
- `api_key` (String) API key.
- `api_key` (String, Sensitive) API key.
- `api_path` (String) API path.
- `base_url` (String) Base URL.
- `categories` (Set of Number) Series list.
Expand Down
6 changes: 3 additions & 3 deletions docs/resources/notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ resource "radarr_notification" "example" {
- `access_token` (String) Access token.
- `access_token_secret` (String) Access token secret.
- `always_update` (Boolean) Always update flag.
- `api_key` (String) API key.
- `app_token` (String) App token.
- `api_key` (String, Sensitive) API key.
- `app_token` (String, Sensitive) App token.
- `arguments` (String) Arguments.
- `auth_password` (String, Sensitive) Password.
- `auth_token` (String) Auth token.
Expand All @@ -71,7 +71,7 @@ resource "radarr_notification" "example" {
- `click_url` (String) Click URL.
- `configuration_key` (String, Sensitive) Configuration key.
- `consumer_key` (String) Consumer key.
- `consumer_secret` (String) Consumer secret.
- `consumer_secret` (String, Sensitive) Consumer secret.
- `device_ids` (Set of String) Device IDs.
- `device_names` (String) Device names.
- `devices` (Set of String) Devices.
Expand Down
9 changes: 9 additions & 0 deletions internal/helpers/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"golang.org/x/exp/slices"
)

const SensitiveValue = "********"

type fieldException struct {
apiName string
tfName string
Expand Down Expand Up @@ -341,6 +343,13 @@ func WriteFields(ctx context.Context, fieldContainer interface{}, fields []*rada
// Loop over each field and populate the related container field with the corresponding write function.
for _, f := range fields {
fieldName := f.GetName()
// Manage sensitive data.
if f.GetValue() == SensitiveValue {
if tempField := readStringField(fieldName, fieldContainer); tempField.GetValue() != nil {
f = tempField
}
}

for listName, writeFunc := range writeFuncs {
if slices.Contains(fieldLists.getList(listName), fieldName) {
writeFunc(f, fieldContainer)
Expand Down
13 changes: 13 additions & 0 deletions internal/helpers/fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,12 @@ func TestWriteFields(t *testing.T) {
value: append(make([]interface{}, 0), []string{"test1", "test2"}),
fieldContainer: Test{Set: types.SetValueMust(types.StringType, nil)},
},
"sensitive": {
fieldLists: Fields{Strings: []string{"str"}},
name: "str",
value: SensitiveValue,
fieldContainer: Test{Str: types.StringValue("String")},
},
}
for name, test := range tests {
test := test
Expand All @@ -624,6 +630,13 @@ func TestWriteFields(t *testing.T) {
fields[0].SetValue(test.value)

container := Test{}
if test.value == SensitiveValue {
// emulate the sensitive behaviour
container = Test{
Str: types.StringValue("String"),
}
}

WriteFields(context.TODO(), &container, fields, test.fieldLists)
assert.Equal(t, &test.fieldContainer, &container)
})
Expand Down
1 change: 1 addition & 0 deletions internal/provider/download_client_aria2_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func (r *DownloadClientAria2Resource) Schema(_ context.Context, _ resource.Schem
MarkdownDescription: "Secret token.",
Optional: true,
Computed: true,
Sensitive: true,
},
},
}
Expand Down
7 changes: 4 additions & 3 deletions internal/provider/download_client_freebox_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ func TestAccDownloadClientFreeboxResource(t *testing.T) {
},
// ImportState testing
{
ResourceName: "radarr_download_client_freebox.test",
ImportState: true,
ImportStateVerify: true,
ResourceName: "radarr_download_client_freebox.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"app_token"},
},
// Delete testing automatically occurs in TestCase
},
Expand Down
7 changes: 4 additions & 3 deletions internal/provider/download_client_hadouken_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func TestAccDownloadClientHadoukenResource(t *testing.T) {
},
// ImportState testing
{
ResourceName: "radarr_download_client_hadouken.test",
ImportState: true,
ImportStateVerify: true,
ResourceName: "radarr_download_client_hadouken.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"password"},
},
// Delete testing automatically occurs in TestCase
},
Expand Down
1 change: 1 addition & 0 deletions internal/provider/download_client_nzbvortex_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func (r *DownloadClientNzbvortexResource) Schema(_ context.Context, _ resource.S
"api_key": schema.StringAttribute{
MarkdownDescription: "API key.",
Required: true,
Sensitive: true,
},
},
}
Expand Down
7 changes: 4 additions & 3 deletions internal/provider/download_client_nzbvortex_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func TestAccDownloadClientNzbvortexResource(t *testing.T) {
},
// ImportState testing
{
ResourceName: "radarr_download_client_nzbvortex.test",
ImportState: true,
ImportStateVerify: true,
ResourceName: "radarr_download_client_nzbvortex.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"api_key"},
},
// Delete testing automatically occurs in TestCase
},
Expand Down
26 changes: 25 additions & 1 deletion internal/provider/download_client_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ func (r *DownloadClientResource) Schema(_ context.Context, _ resource.SchemaRequ
MarkdownDescription: "API key.",
Optional: true,
Computed: true,
Sensitive: true,
},
"rpc_path": schema.StringAttribute{
MarkdownDescription: "RPC path.",
Expand Down Expand Up @@ -345,6 +346,7 @@ func (r *DownloadClientResource) Schema(_ context.Context, _ resource.SchemaRequ
MarkdownDescription: "Secret token.",
Optional: true,
Computed: true,
Sensitive: true,
},
"username": schema.StringAttribute{
MarkdownDescription: "Username.",
Expand Down Expand Up @@ -465,13 +467,14 @@ func (r *DownloadClientResource) Create(ctx context.Context, req resource.Create
// this is needed because of many empty fields are unknown in both plan and read
var state DownloadClient

state.writeSensitive(client)
state.write(ctx, response, &resp.Diagnostics)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}

func (r *DownloadClientResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Get current state
var client DownloadClient
var client *DownloadClient

resp.Diagnostics.Append(req.State.Get(ctx, &client)...)

Expand All @@ -492,6 +495,7 @@ func (r *DownloadClientResource) Read(ctx context.Context, req resource.ReadRequ
// this is needed because of many empty fields are unknown in both plan and read
var state DownloadClient

state.writeSensitive(client)
state.write(ctx, response, &resp.Diagnostics)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
Expand Down Expand Up @@ -521,6 +525,7 @@ func (r *DownloadClientResource) Update(ctx context.Context, req resource.Update
// this is needed because of many empty fields are unknown in both plan and read
var state DownloadClient

state.writeSensitive(client)
state.write(ctx, response, &resp.Diagnostics)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
Expand Down Expand Up @@ -588,3 +593,22 @@ func (d *DownloadClient) read(ctx context.Context, diags *diag.Diagnostics) *rad

return client
}

// writeSensitive copy sensitive data from another resource.
func (d *DownloadClient) writeSensitive(client *DownloadClient) {
if !client.Password.IsUnknown() {
d.Password = client.Password
}

if !client.APIKey.IsUnknown() {
d.APIKey = client.APIKey
}

if !client.SecretToken.IsUnknown() {
d.SecretToken = client.SecretToken
}

if !client.AppToken.IsUnknown() {
d.AppToken = client.AppToken
}
}
23 changes: 22 additions & 1 deletion internal/provider/download_client_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func TestAccDownloadClientResource(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
},
{
ResourceName: "radarr_download_client.test_sensitive",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"password"},
},
// Delete testing automatically occurs in TestCase
},
})
Expand All @@ -64,5 +70,20 @@ func testAccDownloadClientResourceConfig(name, enable string) string {
host = "transmission"
url_base = "/transmission/"
port = 9091
}`, enable, name)
}
resource "radarr_download_client" "test_sensitive" {
enable = false
priority = 1
name = "%sWithSensitive"
host = "hadouken"
url_base = "/hadouken/"
port = 9091
category = "sonarr-tv"
username = "username"
password = "password"
protocol = "torrent"
config_contract = "HadoukenSettings"
implementation = "Hadouken"
}`, enable, name, name)
}
7 changes: 4 additions & 3 deletions internal/provider/download_client_sabnzbd_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func TestAccDownloadClientSabnzbdResource(t *testing.T) {
},
// ImportState testing
{
ResourceName: "radarr_download_client_sabnzbd.test",
ImportState: true,
ImportStateVerify: true,
ResourceName: "radarr_download_client_sabnzbd.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"api_key"},
},
// Delete testing automatically occurs in TestCase
},
Expand Down
7 changes: 4 additions & 3 deletions internal/provider/import_list_couch_potato_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func TestAccImportListCouchPotatoResource(t *testing.T) {
},
// ImportState testing
{
ResourceName: "radarr_import_list_couch_potato.test",
ImportState: true,
ImportStateVerify: true,
ResourceName: "radarr_import_list_couch_potato.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"api_key"},
},
// Delete testing automatically occurs in TestCase
},
Expand Down
7 changes: 4 additions & 3 deletions internal/provider/import_list_radarr_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func TestAccImportListRadarrResource(t *testing.T) {
},
// ImportState testing
{
ResourceName: "radarr_import_list_radarr.test",
ImportState: true,
ImportStateVerify: true,
ResourceName: "radarr_import_list_radarr.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"api_key"},
},
// Delete testing automatically occurs in TestCase
},
Expand Down
10 changes: 10 additions & 0 deletions internal/provider/import_list_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ func (r *ImportListResource) Create(ctx context.Context, req resource.CreateRequ
// this is needed because of many empty fields are unknown in both plan and read
var state ImportList

state.writeSensitive(importList)
state.write(ctx, response, &resp.Diagnostics)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
Expand Down Expand Up @@ -528,6 +529,7 @@ func (r *ImportListResource) Read(ctx context.Context, req resource.ReadRequest,
// this is needed because of many empty fields are unknown in both plan and read
var state ImportList

state.writeSensitive(importList)
state.write(ctx, response, &resp.Diagnostics)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
Expand Down Expand Up @@ -557,6 +559,7 @@ func (r *ImportListResource) Update(ctx context.Context, req resource.UpdateRequ
// this is needed because of many empty fields are unknown in both plan and read
var state ImportList

state.writeSensitive(importList)
state.write(ctx, response, &resp.Diagnostics)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
Expand Down Expand Up @@ -631,3 +634,10 @@ func (i *ImportList) read(ctx context.Context, diags *diag.Diagnostics) *radarr.

return list
}

// writeSensitive copy sensitive data from another resource.
func (i *ImportList) writeSensitive(importList *ImportList) {
if !importList.APIKey.IsUnknown() {
i.APIKey = importList.APIKey
}
}
7 changes: 4 additions & 3 deletions internal/provider/import_list_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func TestAccImportListResource(t *testing.T) {
},
// ImportState testing
{
ResourceName: "radarr_import_list.test",
ImportState: true,
ImportStateVerify: true,
ResourceName: "radarr_import_list.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"api_key"},
},
// Delete testing automatically occurs in TestCase
},
Expand Down
Loading

0 comments on commit a799aa4

Please sign in to comment.