Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create and populate htpasswd file if missing #2362

Merged
merged 1 commit into from
Aug 31, 2018

Conversation

liron-l
Copy link

@liron-l liron-l commented Aug 13, 2017

If htpasswd authentication option is configured but the htpasswd file is
missing, populate it with a default user and automatically generated
password.
The password will be printed to stdout.

Signed-off-by: Liron Levin [email protected]

liron-l pushed a commit to twistlock/distribution-library-image that referenced this pull request Aug 13, 2017
Replaced the default registry auth configuration from 'none' to
'htpasswd'.
Following the change in distribution/distribution#2362.

Signed-off-by: Liron Levin <[email protected]>
@codecov
Copy link

codecov bot commented Aug 13, 2017

Codecov Report

Merging #2362 into master will decrease coverage by 9.63%.
The diff coverage is 45%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2362      +/-   ##
==========================================
- Coverage   60.34%   50.71%   -9.64%     
==========================================
  Files         126      126              
  Lines       14436    14472      +36     
==========================================
- Hits         8712     7340    -1372     
- Misses       4841     6381    +1540     
+ Partials      883      751     -132
Impacted Files Coverage Δ
registry/handlers/app.go 48.3% <100%> (ø) ⬆️
registry/auth/htpasswd/access.go 55.67% <43.58%> (-8.27%) ⬇️
registry/storage/driver/gcs/gcs.go 0.32% <0%> (-66.13%) ⬇️
registry/storage/driver/oss/oss.go 0.45% <0%> (-56.5%) ⬇️
registry/storage/driver/s3-aws/s3.go 4.07% <0%> (-55.4%) ⬇️
registry/storage/driver/s3-goamz/s3.go 0.4% <0%> (-52.4%) ⬇️
registry/client/transport/transport.go 68.75% <0%> (-13.75%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3800056...c785740. Read the comment docs.

@liron-l
Copy link
Author

liron-l commented Aug 15, 2017

CC @dmcgowan

auth:
htpasswd:
realm: basic-realm
path: /auth/htpasswd
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would prefer to use /var/lib/registry or something under /etc for the examples

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I've put it under /etc/registry

if _, err := f.Write([]byte(fmt.Sprintf("docker:%s", string(encryptedPass[:])))); err != nil {
return err
}
logrus.Warnf("htpasswd is missing. provisioned with default user:docker password: %s", pass)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This output looks kind of off

WARN[0000] htpasswd is missing. provisioned with default user:docker password: qakOn184C_WYgnUt7RDyE3xO7YrDkYhpqA9A2JbwIz0=

Maybe just getting rid of the space after password: would make it look more consistent. Also please use RawURLEncoding to get rid of the = at the end, some might find it confusing as to whether it is part of the password or part of an encoding.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I've removed the space and replaced the encoding method.

@liron-l
Copy link
Author

liron-l commented Aug 15, 2017

Thanks @dmcgowan, I've updated the review.

liron-l pushed a commit to twistlock/distribution-library-image that referenced this pull request Aug 15, 2017
Replaced the default registry auth configuration from 'none' to
'htpasswd'.
Following the change in distribution/distribution#2362.

Signed-off-by: Liron Levin <[email protected]>
@liron-l
Copy link
Author

liron-l commented Aug 27, 2017

@dmcgowan PTAL

@liron-l
Copy link
Author

liron-l commented Sep 7, 2017

@dmcgowan anything else I need to change?

@liron-l
Copy link
Author

liron-l commented Oct 2, 2017

@tiborvass @dmcgowan PTAL.

@endophage
Copy link

@tiborvass @stevvooe @dmcgowan what's required to get this merged?

@@ -111,6 +119,34 @@ func (ch challenge) Error() string {
return fmt.Sprintf("basic authentication challenge for realm %q: %s", ch.realm, ch.err)
}

// createHtpasswdFile creates and populates htpasswd file with a new user in case the file is missing
func createHtpasswdFile(path string) error {
if _, err := os.Open(path); os.IsNotExist(err) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put the bulk of the work in the main path, not the indented path.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any other non-empty error should also be returned

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @stevvooe @dmcgowan fixed.

if _, err := f.Write([]byte(fmt.Sprintf("docker:%s", string(encryptedPass[:])))); err != nil {
return err
}
logrus.Warnf("htpasswd is missing. provisioned with default user:docker password:%s", pass)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the logging system already used throughout the registry. We don't access logrus directly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, use the field-based logging for these values, rather than just dumping them in the message.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @stevvooe can I use GetLoggerWithFields? if so, fixed.

@dmcgowan dmcgowan added this to the Registry/2.7 milestone Oct 9, 2017
@liron-l liron-l force-pushed the populate_htpasswd branch 3 times, most recently from fdcbea8 to da7d5e3 Compare October 9, 2017 18:20
@liron-l
Copy link
Author

liron-l commented Oct 9, 2017

Thanks @stevvooe @dmcgowan @endophage I've updated the PR according to your comments.

if _, err := f.Write([]byte(fmt.Sprintf("docker:%s", string(encryptedPass[:])))); err != nil {
return err
}
dcontext.GetLoggerWithFields(context.Background(), map[interface{}]interface{}{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this message should explain the "escape hatch", as this will confusing to those who have already ran the registry. I'll discuss the "escape hatch" more on distribution/distribution-library-image#58.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stevvooe but this config only apply if you start the registry with htpasswd enabled.
If you use REGISTRY_AUTH="" (or silly with realm) it is not enabled.

Is it OK to write
Disable default basic authentication by overriding the 'REGISTRY_AUTH' environment variable?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liron-l If you are using the registry behind a proxy, which is an extremely common deployment, this will break them and those users will panic. What do they do when this happens?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stevvooe I think the scenario you describe is relevant to the hub image change (which is indeed a breaking change).
However, here we just automatically populate the username/password when they are missing in basic auth scenario (like Jenkins does on default setup).

@dmcgowan
Copy link
Collaborator

Can you rebase this to get CI passing, circleci does not seem to be building from a merged branch even when I kicked it to run without cache

@liron-l
Copy link
Author

liron-l commented Oct 11, 2017

Thanks @dmcgowan, The tests passed now.
I'm not sure the code coverage check is related to my change (they appear to be failing in other PRs)

Copy link
Collaborator

@dmcgowan dmcgowan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

If htpasswd authentication option is configured but the htpasswd file is
missing, populate it with a default user and automatically generated
password.
The password will be printed to stdout.

Signed-off-by: Liron Levin <[email protected]>
@jstoja
Copy link
Contributor

jstoja commented Aug 6, 2018

Is this still okay for you @dmcgowan?

Copy link
Contributor

@dmp42 dmp42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dmp42 dmp42 merged commit 90705d2 into distribution:master Aug 31, 2018
@dmp42
Copy link
Contributor

dmp42 commented Aug 31, 2018

@dmcgowan we broke something here. New PRs are not passing failing on gometalinter checks.

gometalinter --config .gometalinter.json ./...
registry/auth/htpasswd/access.go:1::warning: file is not goimported (goimports)
make: *** [check] Error 1

@dmcgowan
Copy link
Collaborator

dmcgowan commented Sep 4, 2018

We cannot merge old PRs that haven't run Travis, we can kick it off by opening a dummy PR with the change commit or having them rebased

@dmp42
Copy link
Contributor

dmp42 commented Sep 5, 2018

@dmcgowan agreed.
My bad on this one.

@davidswu davidswu mentioned this pull request Sep 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants