Skip to content

Commit e4620de

Browse files
authored
auth: improve overall auth system (#54)
- remove password param - add docs on authentication fixes #52 Signed-off-by: Nick Santos <[email protected]>
1 parent f137f6a commit e4620de

File tree

4 files changed

+50
-33
lines changed

4 files changed

+50
-33
lines changed

README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This project is used to manage Docker resources (such as repositories, teams, or
1212

1313
## Usage
1414

15-
Below is a basic example of how to use the Docker services Terraform provider to create a Docker repository. Using `DOCKER_USERNAME` and `DOCKER_PASSWORD` as an environment variable, you can use the following code:
15+
Below is a basic example of how to use the Docker services Terraform provider to create a Docker repository.
1616

1717
```hcl
1818
terraform {
@@ -33,6 +33,48 @@ resource "docker_repository" "example" {
3333
}
3434
```
3535

36+
## Authentication
37+
38+
We have multiple ways to set your Docker credentials.
39+
40+
### Setting credentials
41+
42+
Use `docker login` to [log in to a
43+
registry](https://docs.docker.com/reference/cli/docker/login/). The `docker` CLI
44+
will store your credentials securely in your credential store, such as the
45+
operating system native keychain. The Docker Terraform provider will
46+
use these credentials automatically.
47+
48+
```
49+
cat ~/my_password.txt | docker login --username my-username --password-stdin
50+
```
51+
52+
If you'd like to use a different account for running the provider,
53+
you can set credentials in the environment:
54+
55+
```
56+
export DOCKER_USERNAME=my-username
57+
export DOCKER_PASSWORD=my-secret-token
58+
terraform plan ...
59+
```
60+
61+
### Credential types
62+
63+
You can create a personal access token (PAT) to use as an alternative to your
64+
password for Docker CLI authentication.
65+
66+
A "Read, Write, & Delete" PAT can be used to create, edit, and
67+
manage permissions for Docker Hub repositories.
68+
69+
The advantage of PATs is that they have [many security
70+
benefits](https://docs.docker.com/security/for-developers/access-tokens/) over
71+
passwords.
72+
73+
Unfortunately, PATs are limited to managing repositories. If you'd like to use
74+
this provider to manage organizations and teams, you will need to authenticate
75+
with a password.
76+
77+
3678
## Contributing
3779

3880
We welcome contributions to the Docker services Terraform provider, detailed documentation for contributing & building the provider can be found [here](https://github.com/docker/terraform-provider-docker/blob/main/CONTRIBUTING.md)

docs/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ description: |-
1818
### Optional
1919

2020
- `host` (String) Docker Hub API Host. Default is `hub.docker.com`.
21-
- `password` (String, Sensitive) Password for authentication
2221
- `username` (String) Username for authentication
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
---
22
# generated by https://github.com/hashicorp/terraform-plugin-docs
3-
page_title: "docker_org_team_member_association Resource - docker"
3+
page_title: "docker_org_team_member Resource - docker"
44
subcategory: ""
55
description: |-
66
Manages team members associated with an organization.
77
~> Note Only available when authenticated with a username and password as an owner of the org.
88
---
99

10-
# docker_org_team_member_association (Resource)
10+
# docker_org_team_member (Resource)
1111

1212
Manages team members associated with an organization.
13-
1413
~> **Note** Only available when authenticated with a username and password as an owner of the org.
1514

1615

@@ -22,8 +21,8 @@ Manages team members associated with an organization.
2221

2322
- `org_name` (String) Organization name
2423
- `team_name` (String) Team name
25-
- `user_names` (List of String) User names to be added to the team
24+
- `user_name` (String) User name to be added to the team
2625

2726
### Read-Only
2827

29-
- `id` (String) The ID of the team member association
28+
- `id` (String) The ID of the team member

internal/provider/provider.go

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ type DockerProvider struct {
4444
// DockerProviderModel describes the provider data model.
4545
type DockerProviderModel struct {
4646
Username types.String `tfsdk:"username"`
47-
Password types.String `tfsdk:"password"`
4847
Host types.String `tfsdk:"host"`
4948
}
5049

@@ -67,11 +66,6 @@ func (p *DockerProvider) Schema(ctx context.Context, req provider.SchemaRequest,
6766
MarkdownDescription: "Username for authentication",
6867
Optional: true,
6968
},
70-
"password": schema.StringAttribute{
71-
MarkdownDescription: "Password for authentication",
72-
Optional: true,
73-
Sensitive: true,
74-
},
7569
},
7670
}
7771
}
@@ -98,17 +92,7 @@ func (p *DockerProvider) Configure(ctx context.Context, req provider.ConfigureRe
9892
resp.Diagnostics.AddAttributeError(
9993
path.Root("username"),
10094
"Unknown Docker Hub API Username",
101-
"The provider cannot create the Docker Hub API client as there is an unknown configuration value for the Docker Hub API username. "+
102-
"Either target apply the source of the value first, set the value statically in the configuration, or use the DOCKER_USERNAME environment variable.",
103-
)
104-
}
105-
106-
if data.Password.IsUnknown() {
107-
resp.Diagnostics.AddAttributeError(
108-
path.Root("password"),
109-
"Unknown Docker Hub API Password",
110-
"The provider cannot create the Docker Hub API client as there is an unknown configuration value for the Docker Hub API password. "+
111-
"Either target apply the source of the value first, set the value statically in the configuration, or use the DOCKER_PASSWORD environment variable.",
95+
"The provider cannot create the Docker Hub API client as there is an unknown configuration value for the Docker Hub API username.",
11296
)
11397
}
11498

@@ -132,9 +116,6 @@ func (p *DockerProvider) Configure(ctx context.Context, req provider.ConfigureRe
132116
}
133117

134118
password := os.Getenv("DOCKER_PASSWORD")
135-
if !data.Password.IsNull() {
136-
password = data.Password.ValueString()
137-
}
138119

139120
// If DOCKER_USERNAME and DOCKER_PASSWORD are not set, or if they are empty,
140121
// retrieve them from the credential store
@@ -181,19 +162,15 @@ func (p *DockerProvider) Configure(ctx context.Context, req provider.ConfigureRe
181162
resp.Diagnostics.AddAttributeError(
182163
path.Root("username"),
183164
"Missing Docker Hub API Username",
184-
"The provider cannot create the Docker Hub API client as there is a missing or empty value for the Docker Hub API username. "+
185-
"Set the username value in the configuration or use the DOCKER_USERNAME environment variable. "+
186-
"If either is already set, ensure the value is not empty.",
165+
"Missing valid login credentials. More details: https://github.com/docker/terraform-provider-docker#authentication.",
187166
)
188167
}
189168

190169
if password == "" {
191170
resp.Diagnostics.AddAttributeError(
192171
path.Root("password"),
193172
"Missing Docker Hub API Password",
194-
"The provider cannot create the Docker Hub API client as there is a missing or empty value for the Docker Hub API password. "+
195-
"Set the password value in the configuration or use the DOCKER_PASSWORD environment variable. "+
196-
"If either is already set, ensure the value is not empty.",
173+
"Missing valid login credentials. More details: https://github.com/docker/terraform-provider-docker#authentication.",
197174
)
198175
}
199176

0 commit comments

Comments
 (0)