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

Expose styx origins config file in Admin interface (Issue #496) #520

Merged
merged 2 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright (C) 2013-2019 Expedia Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.admin.handlers;

import com.hotels.styx.api.ByteStream;
import com.hotels.styx.api.Eventual;
import com.hotels.styx.api.HttpHandler;
import com.hotels.styx.api.HttpInterceptor;
import com.hotels.styx.api.LiveHttpRequest;
import com.hotels.styx.api.LiveHttpResponse;

import java.util.function.Supplier;

import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.HttpResponseStatus.OK;
import static com.hotels.styx.api.LiveHttpResponse.response;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;

/**
* Responds with UTF8 text stream.
*/
public class TextHttpHandler implements HttpHandler {

private final Supplier<String> text;

public TextHttpHandler(Supplier<String> text) {
this.text = requireNonNull(text, "text supplier cannot be null");
}

@Override
public Eventual<LiveHttpResponse> handle(LiveHttpRequest request, HttpInterceptor.Context context) {
return Eventual.of(createResponse());
}

protected LiveHttpResponse createResponse() {
return response(OK)
.disableCaching()
.addHeader(CONTENT_TYPE, PLAIN_TEXT_UTF_8)
.body(ByteStream.from(text.get(), UTF_8))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.hotels.styx.services

import com.fasterxml.jackson.databind.JsonNode
import com.hotels.styx.admin.handlers.TextHttpHandler
import com.hotels.styx.api.extension.service.spi.StyxService
import com.hotels.styx.config.schema.SchemaDsl
import com.hotels.styx.config.schema.SchemaDsl.bool
Expand Down Expand Up @@ -56,6 +57,9 @@ internal class YamlFileConfigurationService(

private val healthMonitors = AtomicReference<List<Pair<String, ProviderObjectRecord>>>(listOf())

@Volatile
private var originsConfig = ""

companion object {
@JvmField
val SCHEMA = SchemaDsl.`object`(
Expand All @@ -78,6 +82,10 @@ internal class YamlFileConfigurationService(
LOGGER.info("service stopped")
}

override fun adminInterfaceHandlers() =
mapOf("origins" to TextHttpHandler { originsConfig })


fun reloadAction(content: String): Unit {
LOGGER.info("New origins configuration: \n$content")

Expand All @@ -91,6 +99,8 @@ internal class YamlFileConfigurationService(
}.mapCatching { (healthMonitors, routingObjectDefs) ->
updateRoutingObjects(routingObjectDefs)
updateHealthCheckServices(serviceDb, healthMonitors)
}.onSuccess {
originsConfig = content
initialised.countDown()
}.onFailure {
LOGGER.error("Failed to reload new configuration. cause='{}'", it.message, it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ class OriginsFileCompatibilitySpec : FunSpec() {

init {
context("Styx server starts") {
writeOrigins("""
val originsFile = """
- id: appA
path: "/"
origins:
- { id: "appA-01", host: "localhost:${mockServerA01.port()}" }
""".trimIndent())
""".trimIndent()
writeOrigins(originsFile)
styxServer.restart()

test("It populates forwarding path from the origins yaml file") {
Expand All @@ -102,6 +103,17 @@ class OriginsFileCompatibilitySpec : FunSpec() {
}
}
}

test("The origins config file is returned from the admin service") {
client.send(get("/admin/providers/originsFileLoader/origins")
.header(HOST, styxServer().adminHostHeader())
.build())
.wait()!!
.let {
it.status() shouldBe OK
it.bodyAs(UTF_8) shouldBe originsFile
}
}
}

context("Origins configuration changes") {
Expand Down Expand Up @@ -419,6 +431,14 @@ class OriginsFileCompatibilitySpec : FunSpec() {
}

context("Admin interface") {

val validOriginsFile = """
- id: appA
path: "/"
origins:
- { id: "appA-01", host: "localhost:${mockServerA01.port()}" }
""".trimIndent()

test("Styx dashboard is disabled") {
client.send(get("/")
.header(HOST, styxServer().adminHostHeader())
Expand Down Expand Up @@ -447,6 +467,42 @@ class OriginsFileCompatibilitySpec : FunSpec() {
it.bodyAs(UTF_8) shouldContain """<li><a href='/admin/configuration?pretty'>Configuration</a></li>"""
}
}

test("Modified origins config is returned after update") {

writeOrigins(validOriginsFile)

eventually(2.seconds, AssertionError::class.java) {
client.send(get("/admin/providers/originsFileLoader/origins")
.header(HOST, styxServer().adminHostHeader())
.build())
.wait()!!
.let {
it.status() shouldBe OK
it.bodyAs(UTF_8) shouldBe validOriginsFile
}
}
}

test("Original origins config is returned after invalid update") {

writeOrigins("""
- id: appA
- this file has somehow corrupted
.. bl;ah blah"
""".trimIndent())

eventually(2.seconds, AssertionError::class.java) {
client.send(get("/admin/providers/originsFileLoader/origins")
.header(HOST, styxServer().adminHostHeader())
.build())
.wait()!!
.let {
it.status() shouldBe OK
it.bodyAs(UTF_8) shouldBe validOriginsFile
}
}
}
}

context("Creates a health checking service") {
Expand Down Expand Up @@ -592,12 +648,13 @@ class OriginsFileCompatibilitySpec : FunSpec() {


context("Error scenarios") {
writeOrigins("""
val originsFile = """
- id: appA
path: "/"
origins:
- { id: "appA-01", host: "localhost:${mockServerA01.port()}" }
""".trimIndent())
""".trimIndent()
writeOrigins(originsFile)
styxServer.restart()

test("Keeps the original configuration when a one has problems") {
Expand Down Expand Up @@ -628,12 +685,13 @@ class OriginsFileCompatibilitySpec : FunSpec() {
}

test("Reloads a new configuration after error") {
writeOrigins("""
val newOriginsFile = """
- id: appA
path: "/"
origins:
- { id: "appA-02", host: "localhost:${mockServerA02.port()}" }
""".trimIndent())
""".trimIndent()
writeOrigins(newOriginsFile)

eventually(2.seconds, AssertionError::class.java) {
client.send(get("/20")
Expand Down