Skip to content

Commit

Permalink
config: Use external-address directly for Swagger docs
Browse files Browse the repository at this point in the history
Now `external-address` is used straightforwardly and independently of the TLS
config. And it will be generated from `address` and `tls.enabled`, if not set.

Signed-off-by: Tatiana Nesterenko <[email protected]>
  • Loading branch information
tatiana-nspcc committed Jun 3, 2024
1 parent 5750187 commit 227f373
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ This document outlines major changes between releases.

## [Unreleased]

### Updating from 0.9.0

Notice that the configuration parameter `external-address` in the
`server.endpoints` section now also includes the scheme (http/https), not just
the host and port. If `external-address` is not set, it will be generated from
`address` and `tls.enabled`.

## [0.9.0] - 2024-05-30

### Added
Expand Down
51 changes: 30 additions & 21 deletions cmd/neofs-rest-gw/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,34 @@ func main() {
e.Group(baseURL, middleware.OapiRequestValidator(swagger))
apiserver.RegisterHandlersWithBaseURL(e, neofsAPI, baseURL)

var serverURL string

Check warning on line 65 in cmd/neofs-rest-gw/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-rest-gw/main.go#L65

Added line #L65 was not covered by tests
servers := make(openapi3.Servers, len(serverCfg.Endpoints))
for i, endpointInfo := range serverCfg.Endpoints {
if endpointInfo.ExternalAddress != "" {
var scheme string
// Determine the scheme based on whether TLS is enabled and set up e.TLSServer.
if endpointInfo.TLS.Enabled {
scheme = schemeHTTPS
e.TLSServer.ReadTimeout = endpointInfo.ReadTimeout
e.TLSServer.WriteTimeout = endpointInfo.WriteTimeout
e.TLSServer.IdleTimeout = endpointInfo.KeepAlive

if endpointInfo.TLS.CertCAFile != "" {
ca, err := loadCA(endpointInfo.TLS.CertCAFile)
if err != nil {
logger.Fatal("reading server certificate", zap.Error(err))
}
e.TLSServer.TLSConfig = &tls.Config{ClientCAs: ca}
serverURL = fmt.Sprintf("%s%s", endpointInfo.ExternalAddress, baseURL)
} else {
scheme := getScheme(endpointInfo.TLS.Enabled)
serverURL = fmt.Sprintf("%s://%s%s", scheme, endpointInfo.Address, baseURL)
logger.Info("Endpoint with missing external-address",
zap.String("address", endpointInfo.Address),
zap.String("set external-address", serverURL))

Check warning on line 75 in cmd/neofs-rest-gw/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-rest-gw/main.go#L69-L75

Added lines #L69 - L75 were not covered by tests
}
servers[i] = &openapi3.Server{
URL: serverURL,

Check warning on line 78 in cmd/neofs-rest-gw/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-rest-gw/main.go#L77-L78

Added lines #L77 - L78 were not covered by tests
}

if endpointInfo.TLS.Enabled {
e.TLSServer.ReadTimeout = endpointInfo.ReadTimeout
e.TLSServer.WriteTimeout = endpointInfo.WriteTimeout
e.TLSServer.IdleTimeout = endpointInfo.KeepAlive

Check warning on line 84 in cmd/neofs-rest-gw/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-rest-gw/main.go#L81-L84

Added lines #L81 - L84 were not covered by tests

if endpointInfo.TLS.CertCAFile != "" {
ca, err := loadCA(endpointInfo.TLS.CertCAFile)
if err != nil {
logger.Fatal("reading server certificate", zap.Error(err))

Check warning on line 89 in cmd/neofs-rest-gw/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-rest-gw/main.go#L86-L89

Added lines #L86 - L89 were not covered by tests
}
} else {
scheme = schemeHTTP
}
servers[i] = &openapi3.Server{
URL: fmt.Sprintf("%s://%s%s", scheme, endpointInfo.ExternalAddress, baseURL),
e.TLSServer.TLSConfig = &tls.Config{ClientCAs: ca}

Check warning on line 91 in cmd/neofs-rest-gw/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-rest-gw/main.go#L91

Added line #L91 was not covered by tests
}
} else {
logger.Info("Endpoint with missing external-address", zap.String("address", endpointInfo.Address))
}
}
swagger.Servers = servers
Expand Down Expand Up @@ -137,3 +139,10 @@ func swaggerDocHandler(c echo.Context) error {
func redirectHandler(c echo.Context) error {
return c.Redirect(http.StatusTemporaryRedirect, docsURL)
}

func getScheme(tlsEnabled bool) string {
if tlsEnabled {
return schemeHTTPS

Check warning on line 145 in cmd/neofs-rest-gw/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-rest-gw/main.go#L143-L145

Added lines #L143 - L145 were not covered by tests
}
return schemeHTTP

Check warning on line 147 in cmd/neofs-rest-gw/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-rest-gw/main.go#L147

Added line #L147 was not covered by tests
}
8 changes: 5 additions & 3 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ server:
endpoints:
# The IP and port to listen on.
- address: localhost:8081
# The IP and port to be shown in the API documentation.
external-address: localhost:8091
# The full URL address needs to be shown in the API documentation,
# including the scheme (http/https), host, and port.
# If not set, will be generated from `address` and `tls.enabled`.
external-address: https://localhost:8091
tls:
# Use TLS for a gRPC connection (min version is TLS 1.2).
enabled: true
Expand All @@ -76,7 +78,7 @@ server:
write-timeout: 30s

- address: localhost:8080
external-address: localhost:8090
external-address: http://localhost:8090
tls:
enabled: false
certificate: /path/to/tls/cert
Expand Down
2 changes: 1 addition & 1 deletion docs/gate-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ listen-limit: 0
| `endpoint.[0].tls.certificate` | `string` | | The certificate file to use for secure connections. |
| `endpoint.[0].tls.key` | `string` | | The private key file to use for secure connections (without passphrase). |
| `endpoint.[0].tls.ca` | `string` | | The certificate authority certificate file to be used with mutual tls auth. |
| `endpoint.[0].external-address` | `string` | `localhost:8090` | The IP and port to be shown in the API documentation. |
| `endpoint.[0].external-address` | `string` | | The full URL address needs to be shown in the API documentation, including the scheme (http/https), host, and port. If not set, will be generated from `address` and `tls.enabled`. |

# `wallet` section

Expand Down

0 comments on commit 227f373

Please sign in to comment.