[Python] Can't access Spring Cloud Config from Python

Discussão em 'Python' iniciado por Stack, Setembro 16, 2024.

  1. Stack

    Stack Membro Participativo

    I've created a Spring Cloud Config Server with (for now) only two yaml files

    • application.yml
    • application-dev.yml

    Also Spring Security with basic authentication (username & password) is defined, but curiously I can't access my configurations except from browser (Chrome & Safari).

    Below you can see my configurations.

    • application.yml

    # Basic configs - Name & Profile
    spring:
    application:
    name: EMP_CNF-S
    profiles:
    default: ${ENVIRONMENT}

    # GitHub Repository (private & SSH)
    cloud:
    config:
    server:
    git:
    uri: git@github.com:*******/EMP_CNF-S.git
    ignore-local-ssh-settings: true
    default-label: ${ENVIRONMENT}
    private-key: ${GIT_SSH_KEY}
    search-paths: src/main/resources
    skip-ssl-validation: true
    timeout: 10
    clone-on-start: true

    security:
    user:
    name: ${EMP_CONFIG_USERNAME}
    password: ${EMP_CONFIG_PASSWORD}

    # Configuring Port
    server:
    port: 8888

    # Spring Actuator
    management:
    endpoints:
    web:
    exposure:
    include:
    - health
    - info
    base-path: /actuator
    endpoint:
    health:
    show-details: always


    Here my application-dev.yml (Every variable value was set due to GitHub Actions & GitHub Secrets)

    # Common Configurations - Datasource
    spring:
    datasource:
    url: jdbc:mysql://localhost:3306/
    username: root
    password: *******

    jpa:
    hibernate:
    ddl-auto: update
    show-sql: true

    test:
    database:
    replace: none

    # Location Service
    location-service:
    schema: location_db

    # Employee Service
    employee-service:
    schema: employee_db

    # Customer Service
    customer-service:
    schema: ${EMP_SCHEMA_CUSTOMER}

    # Supplier Service
    supplier-service:
    schema: ${EMP_SCHEMA_SUPPLIER}


    But when I try to access my environment variables (location_db) I can't. I do get no result when trying to "curl" it:

    curl -u admin:****** http://localhost:8888/location-service/dev


    Also in my python script when using module spring-config-client and even with requests library I can't access the values despite getting a 200 success code. Here a snippet below:

    import requests
    from requests.auth import HTTPBasicAuth

    url = "http://localhost:8888/location-service/dev"
    response = requests.get(url=url, auth=HTTPBasicAuth(username='admin', password='******'), headers=headers)

    # Print the raw content
    print(f"Response status code: {response.status_code}")
    print(f"Response text: {response.text}")

    # Attempt to parse as JSON
    try:
    config = response.json()
    print(config)
    except requests.exceptions.JSONDecodeError as e:
    print(f"JSON decode error: {e}")


    Output:

    Response status code: 200
    Response text: <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Please sign in</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
    <link href="https://getbootstrap.com/docs/4.0/examples/signin/signin.css" rel="stylesheet" integrity="sha384-oOE/3m0LUMPub4kaC09mrdEhIc+e3exm4xOGxAmuFXhBNF4hcg/6MiAXAf5p0P56" crossorigin="anonymous"/>
    </head>
    <body>
    <div class="container">
    <form class="form-signin" method="post" action="/login">
    <h2 class="form-signin-heading">Please sign in</h2>
    <p>
    <label for="username" class="sr-only">Username</label>
    <input type="text" id="username" name="username" class="form-control" placeholder="Username" required autofocus>
    </p>
    <p>
    <label for="password" class="sr-only">Password</label>
    <input type="password" id="password" name="password" class="form-control" placeholder="Password" required>
    </p>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
    </form>
    </div>
    </body></html>
    JSON decode error: Expecting value: line 1 column 1 (char 0)


    Here also the usage of spring_config module:

    from spring_config import ClientConfigurationBuilder
    from spring_config.client import SpringConfigClient

    spring_config_username = os.getenv(key = 'EMP_CONFIG_USERNAME')
    spring_config_password = '****'

    # Build the client configuration
    config = (
    ClientConfigurationBuilder()
    .app_name("location-service")
    .profile("dev")
    .branch('dev')
    .address("http://localhost:8888")
    .authentication((spring_config_username, spring_config_password)) # Set the authentication
    .build()
    )

    # Create the Spring Config Client
    client = SpringConfigClient(config)


    And here the (error) output:

    File "/Users/acharrade/.pyenv/versions/3.12.2/lib/python3.12/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

    Continue reading...

Compartilhe esta Página