Skip to content

chore(tooling): create a HTTP Service to test HTTP IdentityProvider #5419

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
39 changes: 39 additions & 0 deletions gravitee-am-tooling/gravitee-am-tooling-http-provider/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Build Docker image

```
mvn package com.google.cloud.tools:jib-maven-plugin:dockerBuild -Dimage=<image name>
```

# Run Image

The image is configured to bind on 0.0.0.0 and listen the port 8080.

# Request

Authenticate user using password (in clear text)

```
curl -vv -X POST http://localhost:8080/login -d '{"username":"user01", "password":"Test1234567!" }' -H'Content-Type: application/json'

{
"username" : "user01",
"preferred_username" : "alice",
"given_name" : "Wonder",
"first_name" : "Alice",
"email" : "[email protected]"
}
```

Get profile by username without password

```
curl -vv -X POST http://localhost:8080/username -d '{"username":"user01"}' -H'Content-Type: application/json'

{
"username" : "user01",
"preferred_username" : "alice",
"given_name" : "Wonder",
"first_name" : "Alice",
"email" : "[email protected]"
}
```
93 changes: 93 additions & 0 deletions gravitee-am-tooling/gravitee-am-tooling-http-provider/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (C) 2015 The Gravitee team (http://gravitee.io)

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.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.gravitee.am</groupId>
<artifactId>gravitee-am-tooling</artifactId>
<version>4.7.0-SNAPSHOT</version>
</parent>

<artifactId>gravitee-am-tooling-http-provider</artifactId>
<name>Gravitee IO - Access Management - Tooling - HTTP Provider</name>

<properties>
<jib-maven-plugin.version>3.4.2</jib-maven-plugin.version>
</properties>

<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</dependency>
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>nimbus-jose-jwt</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>${jib-maven-plugin.version}</version>
<configuration>
<to>
<image>localhost:5000/gravitee-am-fapi-resource</image>
</to>
<from>
<image>eclipse-temurin:17</image>
</from>
<container>
<jvmFlags>
<jvmFlag>-Xmx64m</jvmFlag>
</jvmFlags>
<mainClass>io.gravitee.am.tooling.http.HttpProviderApi</mainClass>
<ports>
<port>8080</port>
</ports>
<format>OCI</format>
</container>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* 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 io.gravitee.am.tooling.http;

import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.StaticHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;

import static io.vertx.core.http.HttpMethod.POST;

/**
* @author Eric LELEU (eric.leleu at graviteesource.com)
*/
public class HttpProviderApi {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpProviderApi.class);

public static final String CONF_HOST = "host";
public static final String CONF_PORT = "port";
public static final String CONF_TRUST_STORE_PATH = "trustStorePath";
public static final String CONF_TRUST_STORE_TYPE = "trustStoreType";
public static final String CONF_TRUST_STORE_PASSWORD = "trustStorePassword";
public static final String CONF_KEY_STORE_PATH = "keyStorePath";
public static final String CONF_KEY_STORE_TYPE = "keyStoreType";
public static final String CONF_KEY_STORE_PASSWORD = "keyStorePassword";
public static final String CONF_CERT_HEADER = "certificateHeader";

public static void main(String[] args) throws Exception {
HttpServerOptions options = buildHttpOptions();

Vertx vertx = Vertx.vertx();
HttpServer server = vertx.createHttpServer(options);

Router router = Router.router(vertx);
router.route()
.handler(StaticHandler.create());

router.route("/login")
.method(POST)
.consumes("application/json")
.produces("application/json")
.handler(BodyHandler.create())
.handler(new LoginHandler(true));

router.route("/username")
.method(POST)
.consumes("application/json")
.produces("application/json")
.handler(BodyHandler.create())
.handler(new LoginHandler(false));

server.requestHandler(router).listen();
LOGGER.info("Server listening on port {}", options.getPort());
}

private static HttpServerOptions buildHttpOptions() {
Integer httpProviderPort = Optional.ofNullable(System.getenv("HTTP_PROVIDER_PORT")).map(Integer::parseInt).orElse(8080);
HttpServerOptions options = new HttpServerOptions();
options.setPort(httpProviderPort);
options.setHost("0.0.0.0");
options.setUseAlpn(false);
return options;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* 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 io.gravitee.am.tooling.http;

import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.MissingResourceException;
import java.util.Optional;
import java.util.ResourceBundle;


public class LoginHandler implements Handler<RoutingContext> {
private static final Logger LOGGER = LoggerFactory.getLogger(LoginHandler.class);

private final boolean comparePassword;

public LoginHandler(boolean comparePassword) {
this.comparePassword = comparePassword;
}

@Override
public void handle(RoutingContext routingContext) {
try {
final var usersBundle = ResourceBundle.getBundle("users");

final var payload = routingContext.body().asJsonObject();
final var username = payload.getString("username");
final var passwordRef = usersBundle.getString(username +".password");

if (passwordRef == null || "".equals(passwordRef.trim()) || (comparePassword && !passwordRef.equals(payload.getString("password")))) {
routingContext.response()
.putHeader("content-type", "application/json")
.setStatusCode(401).end();
}

if (!comparePassword && username.equalsIgnoreCase("user03")) {
routingContext.response()
.putHeader("content-type", "application/json")
.setStatusCode(500).end(JsonObject.of("error", "Service Not Available").encodePrettily());
}

final JsonObject responsePayload = new JsonObject();
responsePayload.put("username", username);
responsePayload.put("sub", username);
Optional.ofNullable(usersBundle.getString(username +".preferred_username")).ifPresent(value -> responsePayload.put("preferred_username", value));
Optional.ofNullable(usersBundle.getString(username +".given_name")).ifPresent(value -> responsePayload.put("given_name", value));
Optional.ofNullable(usersBundle.getString(username +".first_name")).ifPresent(value -> responsePayload.put("first_name", value));
Optional.ofNullable(usersBundle.getString(username +".email")).ifPresent(value -> responsePayload.put("email", value));

//response ok
final int statusCode = 200;
routingContext.response()
.putHeader("content-type", "application/json")
.setStatusCode(statusCode)
.end(responsePayload.encodePrettily()); // return JWT as JSON object
} catch (MissingResourceException e) {
LOGGER.info("Username not found", e);
routingContext.fail(401, e);
} catch (Exception e) {
LOGGER.warn("Unable to process the FAPI resource request", e);
routingContext.fail(500, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
user01.password=Test1234567!
user01.preferred_username=alice
user01.given_name=Wonder
user01.first_name=Alice
[email protected]
user02.password=Test"1234567"!
user02.preferred_username=jdoe
user02.given_name=Doe
user02.first_name=John
[email protected]
user03.password=Test1234567!
user03.preferred_username=bob
user03.given_name=Dylan
user03.first_name=Bob
[email protected]
35 changes: 35 additions & 0 deletions gravitee-am-tooling/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!--

Copyright (C) 2015 The Gravitee team (http://gravitee.io)

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.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.gravitee.am</groupId>
<artifactId>gravitee-am-parent</artifactId>
<version>4.7.0-SNAPSHOT</version>
</parent>

<artifactId>gravitee-am-tooling</artifactId>
<name>Gravitee IO - Access Management - Tooling</name>
<packaging>pom</packaging>

<modules>
<module>gravitee-am-tooling-http-provider</module>
</modules>

</project>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<module>gravitee-am-ciba-delegated-service</module>
<module>gravitee-am-monitoring</module>
<module>gravitee-am-dataplane</module>
<module>gravitee-am-tooling</module>
</modules>

<properties>
Expand Down