spring-boot-admin icon indicating copy to clipboard operation
spring-boot-admin copied to clipboard

Metadata key "user.name" and "user.password" are not supported by consul

Open Ayden-Franklin opened this issue 6 years ago • 4 comments

The BasicAuthHttpHeaderProvider then uses this metadata to add the Authorization header to access your application’s actuator endpoints. And the code looks like this:

String username = instance.getRegistration().getMetadata().get("user.name");
String password = instance.getRegistration().getMetadata().get("user.password");

As Spring Cloud reference, Consul supports metadata on services. My application.yaml file contains below:

spring:
    cloud:
        consul:
            discovery:
                metadata:
                    user.name: spring_boot
                    userassword: MY_PASSWORD

Unfortunately, consul client library(consul-api-1.4.5.jar) reported an error coming from the consul server.

Caused by: com.ecwid.consul.v1.OperationException: OperationException(statusCode=400, statusMessage='Bad Request', statusContent='Invalid Service Meta: Couldn't load metadata pair ('user.name', 'spring_boot'): Key contains invalid characters')
	at com.ecwid.consul.v1.agent.AgentConsulClient.agentServiceRegister(AgentConsulClient.java:278) ~[consul-api-1.4.5.jar:na]
	at com.ecwid.consul.v1.ConsulClient.agentServiceRegister(ConsulClient.java:310) ~[consul-api-1.4.5.jar:na]
	at org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry.register(ConsulServiceRegistry.java:68) ~[spring-cloud-consul-discovery-2.2.3.RELEASE.jar:2.2.3.RELEASE]
Caused by: com.ecwid.consul.v1.OperationException: OperationException(statusCode=400, statusMessage='Bad Request', statusContent='Invalid Service Meta: Couldn't load metadata pair ('user.name', 'spring_boot'): Key contains invalid characters')

	at org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry.register(ConsulServiceRegistry.java:43) ~[spring-cloud-consul-discovery-2.2.3.RELEASE.jar:2.2.3.RELEASE]
...

As the exception mentioned, the metadata can not contain user.name. Would you like to change the name of key(user.name) or help me to find another better approach than providing my own HttpHeadersProvider? Thank you.

Ayden-Franklin avatar Jun 15 '20 06:06 Ayden-Franklin

Either we could make the keys configurable or search additionally for keys (e.g. "user_name") satisfying the consul constraints (^[a-zA-Z0-9_-]+$). Would you like to work on a PR for it?

joshiste avatar Jun 15 '20 10:06 joshiste

I commented an issue to hashicorp/consul project and hope consul can support "user.name" for Service.Meta. I'll update this issue after his response.

Ayden-Franklin avatar Jun 16 '20 09:06 Ayden-Franklin

For the record, the issue opened in consul by @Macrame: https://github.com/hashicorp/consul/issues/8127

laffuste avatar Jun 18 '20 08:06 laffuste

@joshiste I pushed a PR, feel free to ask for modification.

laffuste avatar Jun 25 '20 10:06 laffuste