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

Listen with TLS if configured #746

Merged
merged 6 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Configuration file path can be selected with the `-config` flag. [#745](https://github.com/elastic/package-registry/pull/745)
* Configuration flags can be provided using environment variables. [#745](https://github.com/elastic/package-registry/pull/745)
* Add -tls-cert and -tls-key flags to configure HTTPS. [#711](https://github.com/elastic/package-registry/issues/711) [#746](https://github.com/elastic/package-registry/issues/746)
jsoriano marked this conversation as resolved.
Show resolved Hide resolved

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ EXPOSE 8080
ENTRYPOINT ["./package-registry"]

# Make sure it's accessible from outside the container
CMD ["--address=0.0.0.0:8080"]
ENV EPR_ADDRESS=0.0.0.0:8080

HEALTHCHECK --interval=1s --retries=30 CMD curl --silent --fail localhost:8080/health || exit 1

13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,18 @@ docker run -p 8080:8080 {image id from prior step}
**Commands ready to cut-and-paste**
```
docker build --rm -t docker.elastic.co/package-registry/package-registry:master .
docker run -i -t -p 8080:8080 $(docker images -q docker.elastic.co/package-registry/package-registry:master)
docker run -it -p 8080:8080 $(docker images -q docker.elastic.co/package-registry/package-registry:master)
```

**Listening on HTTPS**
```
docker run -it -p 8443:8443 \
-v /etc/ssl/package-registry.key:/etc/ssl/package-registry.key:ro \
-v /etc/ssl/package-registry.crt:/etc/ssl/package-registry.crt:ro \
-e EPR_ADDRESS=0.0.0.0:8443
-e EPR_TLS_KEY=/etc/ssl/package-registry.key \
-e EPR_TLS_CERT=/etc/ssl/package-registry.crt \
docker.elastic.co/package-registry/package-registry:master
```

#### Docker images published
Expand Down
14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ var (
address string
httpProfAddress string

tlsCertFile string
tlsKeyFile string

dryRun bool
configPath string

Expand All @@ -50,6 +53,8 @@ var (

func init() {
flag.StringVar(&address, "address", "localhost:8080", "Address of the package-registry service.")
flag.StringVar(&tlsCertFile, "tls-cert", "", "Path of the TLS certificate.")
flag.StringVar(&tlsKeyFile, "tls-key", "", "Path of the TLS key.")
flag.StringVar(&configPath, "config", "config.yml", "Path to the configuration file.")
flag.StringVar(&httpProfAddress, "httpprof", "", "Enable HTTP profiler listening on the given address.")
// This flag is experimental and might be removed in the future or renamed
Expand All @@ -74,7 +79,7 @@ func main() {

server := initServer()
go func() {
err := server.ListenAndServe()
err := runServer(server)
if err != nil && err != http.ErrServerClosed {
log.Fatalf("Error occurred while serving: %s", err)
}
Expand Down Expand Up @@ -130,6 +135,13 @@ func initServer() *http.Server {
return &http.Server{Addr: address, Handler: router}
}

func runServer(server *http.Server) error {
if tlsCertFile != "" && tlsKeyFile != "" {
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
return server.ListenAndServeTLS(tlsCertFile, tlsKeyFile)
}
return server.ListenAndServe()
}

func initAPMTracer() *apm.Tracer {
apm.DefaultTracer.Close()
if _, found := os.LookupEnv("ELASTIC_APM_SERVER_URL"); !found {
Expand Down