Skip to content

Create and populate htpasswd file if missing - #2362

Merged
dmp42 merged 1 commit into
distribution:masterfrom
twistlock:populate_htpasswd
Aug 31, 2018
Merged

Create and populate htpasswd file if missing#2362
dmp42 merged 1 commit into
distribution:masterfrom
twistlock:populate_htpasswd

Conversation

@liron-l

Copy link
Copy Markdown

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 liron@twistlock.com

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 <liron@twistlock.com>
@codecov

codecovBot commented Aug 13, 2017

Copy link
Copy Markdown

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 FilesCoverage Δ
registry/handlers/app.go48.3% <100%> (ø)⬆️
registry/auth/htpasswd/access.go55.67% <43.58%> (-8.27%)⬇️
registry/storage/driver/gcs/gcs.go0.32% <0%> (-66.13%)⬇️
registry/storage/driver/oss/oss.go0.45% <0%> (-56.5%)⬇️
registry/storage/driver/s3-aws/s3.go4.07% <0%> (-55.4%)⬇️
registry/storage/driver/s3-goamz/s3.go0.4% <0%> (-52.4%)⬇️
registry/client/transport/transport.go68.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
Copy Markdown
Author

CC @dmcgowan

Comment threadcmd/registry/config-example.yml Outdated
auth:
htpasswd:
realm: basic-realm
path: /auth/htpasswd

Copy link
Copy Markdown
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
Copy Markdown
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

Comment threadregistry/auth/htpasswd/access.go Outdated
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Author

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 <liron@twistlock.com>
@liron-l

Copy link
Copy Markdown
Author

@dmcgowan PTAL

@liron-l

Copy link
Copy Markdown
Author

@dmcgowan anything else I need to change?

@liron-l

Copy link
Copy Markdown
Author

@tiborvass@dmcgowan PTAL.

@endophage

Copy link
Copy Markdown

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

Comment threadregistry/auth/htpasswd/access.go Outdated

// 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment threadregistry/auth/htpasswd/access.go Outdated
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@dmcgowandmcgowan added this to the Registry/2.7 milestone Oct 9, 2017
@liron-l
liron-lforce-pushed the populate_htpasswd branch 3 times, most recently from fdcbea8 to da7d5e3CompareOctober 9, 2017 18:20
@liron-l

Copy link
Copy Markdown
Author

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Author

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)

@dmcgowandmcgowan left a comment

Copy link
Copy Markdown
Collaborator

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 <liron@twistlock.com>
@jstoja

Copy link
Copy Markdown
Contributor

Is this still okay for you @dmcgowan?

@dmp42dmp42 left a comment

Copy link
Copy Markdown
Contributor

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:masterAug 31, 2018
@dmp42

Copy link
Copy Markdown
Contributor

@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

@dmp42dmp42 mentioned this pull request Aug 31, 2018
@dmcgowan

Copy link
Copy Markdown
Collaborator

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

dmp42 commented Sep 5, 2018

Copy link
Copy Markdown
Contributor

@dmcgowan agreed.
My bad on this one.

@davidswudavidswu mentioned this pull request Sep 5, 2018
Sign up for freeto 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

@liron-l@endophage@dmcgowan@jstoja@dmp42@stevvooe@Qdigital