Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External logging configuration with Spring Cloud Config


I just experienced Spring Cloud Config to have some configuration files outside of my projects. I followed the instructions to set up a client and a server (linked to a git) and it worked great !
Basically I have different application.yml for each profile I have and inside these files there is a server port property and also an URL to the logging config file (one log4j2.yml per profile) which is also on my git repository. Without authentication it works well: the client asks the server for the application.yml file which matches its profile. Then, the server finds the file and return to the client the port and the log4j2 config file.
I have what I want which is a different level of logging depending on the client's profile.
When I set up the authentication with spring-security (with the default username and a simple password) the client recovers the port but when it tries to acess to the log4j2 config file, the server returns a 401 error saying that the client isn't authorized to access this file.
This might be due to the client not knowing the credentials to access the file inside the application.yml and I don't know if this is possible to insert credentials in the logging.config property

I tried something like this but it's not working as well :

logging:
 config: http://user:password@localhost:8888/....../log4j2.yml

There might be an alternative which consists in saying to the server to ignore the security when the URL is that file but if one day I must have authentication to access it, I won't be able to do it.

There's my files:

GIT

application-dev.yml

server:
 port: 55556

logging:
 config: http://localhost:8888/ConfigExtClient/dev/master/log4j2.yml

CLIENT

boostrap.yml

spring:
  application:
    name: ConfigExtClient
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8888
      username: user
      password: foo

Dependencies (pom.xml)

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>1.1.0.M4</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>


    </dependencies>

SERVER

application.yml

server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: URLtoGit

security:
  user:
    name: user
    password: foo

bootstrap.yml

spring:
  application:
    name: ConfigExtServer

dependencies (pom.xml)

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.1.RELEASE</version>
</parent>

<dependencies>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>1.1.0.M4</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

</dependencies> 

ERROR

Logging config file location 'http://localhost:8888/ConfigExtClient/dev/master/log4j2.yml' cannot be opened and will be ignored

I tracked the error and it appears in the PropertySourceBootstrapConfiguration class in reinitializeLoggingSystem :

try {
            ResourceUtils.getURL(logConfig).openStream().close();
            system.initialize(new LoggingInitializationContext(environment),
                    logConfig, logFile);
        }
        catch (Exception ex) {
            PropertySourceBootstrapConfiguration.logger
                    .warn("Logging config file location '" + logConfig
                            + "' cannot be opened and will be ignored");
        }

It goes into catch and the exception is :

Server returned HTTP response code: 401 for URL: http://localhost:8888/ConfigExtClient/dev/master/log4j2.yml

Thank you in advance for your help,
Romain

like image 844
Romain D Avatar asked Feb 09 '16 11:02

Romain D


1 Answers

You can set your config client like this sample in the GitHub.

it needs the log4j2.component.properties and bootstrap.yml configuration...

bootstrap.yml

logging:
  config: http://configServerAddress:8888/yourAppName/yourSpringProfile/gitBranch/log4j2.xml

log4j2.component.properties

log4j.configurationFile=http://configServerAddress:8888/yourAppName/yourSpringProfile/gitBranch/log4j2.xml
log4j2.configurationUserName=guest
log4j2.configurationPassword=guest
like image 59
Mojtaba Avatar answered Oct 29 '22 23:10

Mojtaba