-
Notifications
You must be signed in to change notification settings - Fork 28
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
Enable test mode on API gateway #389
Merged
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5e9c009
Enable admin authentication on API gateway
nicoloboschi 7ad04b1
spotless
nicoloboschi 247e7fd
kebab
nicoloboschi d13cd33
add to all examples
nicoloboschi 213db1b
implement Cli
nicoloboschi 8f70724
spotless
nicoloboschi c0a8df3
change name and others
nicoloboschi 09d1c79
spotless
nicoloboschi 3dd647b
rename & fix example
nicoloboschi e3d6de7
spotless
nicoloboschi 439c164
spotless
nicoloboschi 1c3ed85
spotless
nicoloboschi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
langstream-api-gateway-auth/langstream-http-gateway-admin-auth/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
|
||
Copyright DataStax, 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. | ||
|
||
--> | ||
<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"> | ||
<parent> | ||
<artifactId>langstream-api-gateway-auth</artifactId> | ||
<groupId>ai.langstream</groupId> | ||
<version>0.0.16-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>langstream-http-gateway-admin-auth</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>ai.langstream</groupId> | ||
<artifactId>langstream-api</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
94 changes: 94 additions & 0 deletions
94
...in/java/ai/langstream/apigateway/auth/impl/jwt/admin/HttpAdminAuthenticationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright DataStax, 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 ai.langstream.apigateway.auth.impl.jwt.admin; | ||
|
||
import ai.langstream.api.gateway.GatewayAdminAuthenticationProvider; | ||
import ai.langstream.api.gateway.GatewayAdminRequestContext; | ||
import ai.langstream.api.gateway.GatewayAuthenticationResult; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.time.Duration; | ||
import java.util.Map; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class HttpAdminAuthenticationProvider implements GatewayAdminAuthenticationProvider { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why only "admin", we can make it a regular implementation |
||
|
||
private static final ObjectMapper mapper = new ObjectMapper(); | ||
private HttpAdminAuthenticationProviderConfiguration httpConfiguration; | ||
private HttpClient httpClient; | ||
|
||
@Override | ||
public String type() { | ||
return "http"; | ||
} | ||
|
||
@Override | ||
@SneakyThrows | ||
public void initialize(Map<String, Object> configuration) { | ||
httpConfiguration = | ||
mapper.convertValue( | ||
configuration, HttpAdminAuthenticationProviderConfiguration.class); | ||
httpClient = | ||
HttpClient.newBuilder() | ||
.connectTimeout(Duration.ofSeconds(30)) | ||
.followRedirects(HttpClient.Redirect.ALWAYS) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public GatewayAuthenticationResult authenticate(GatewayAdminRequestContext context) { | ||
|
||
final Map<String, String> placeholders = Map.of("tenant", context.tenant()); | ||
final String uri = resolvePlaceholders(placeholders, httpConfiguration.getPathTemplate()); | ||
final String url = httpConfiguration.getBaseUrl() + uri; | ||
|
||
log.info("Authenticating admin with url: {}", url); | ||
|
||
final HttpRequest.Builder builder = HttpRequest.newBuilder().uri(URI.create(url)); | ||
|
||
httpConfiguration.getHeaders().forEach(builder::header); | ||
builder.header("Authorization", "Bearer " + context.credentials()); | ||
final HttpRequest request = builder.GET().build(); | ||
|
||
final HttpResponse<Void> response; | ||
try { | ||
response = httpClient.send(request, HttpResponse.BodyHandlers.discarding()); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new RuntimeException(e); | ||
} catch (Throwable e) { | ||
return GatewayAuthenticationResult.authenticationFailed(e.getMessage()); | ||
} | ||
if (httpConfiguration.getAcceptedStatuses().contains(response.statusCode())) { | ||
return GatewayAuthenticationResult.authenticationSuccessful( | ||
context.adminCredentialsInputs()); | ||
} | ||
return GatewayAuthenticationResult.authenticationFailed( | ||
"Http authentication failed: " + response.statusCode()); | ||
} | ||
|
||
private static String resolvePlaceholders(Map<String, String> placeholders, String url) { | ||
for (Map.Entry<String, String> entry : placeholders.entrySet()) { | ||
url = url.replace("{" + entry.getKey() + "}", entry.getValue()); | ||
} | ||
return url; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...ngstream/apigateway/auth/impl/jwt/admin/HttpAdminAuthenticationProviderConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright DataStax, 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 ai.langstream.apigateway.auth.impl.jwt.admin; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAlias; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class HttpAdminAuthenticationProviderConfiguration { | ||
|
||
@JsonAlias("base-url") | ||
private String baseUrl; | ||
|
||
@JsonAlias("path-template") | ||
private String pathTemplate; | ||
|
||
private Map<String, String> headers = new HashMap<>(); | ||
|
||
@JsonAlias("accepted-statuses") | ||
private List<Integer> acceptedStatuses = List.of(200, 201); | ||
} |
1 change: 1 addition & 0 deletions
1
.../resources/META-INF/services/ai.langstream.api.gateway.GatewayAdminAuthenticationProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ai.langstream.apigateway.auth.impl.jwt.admin.HttpAdminAuthenticationProvider |
43 changes: 43 additions & 0 deletions
43
langstream-api-gateway-auth/langstream-jwt-gateway-admin-auth/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
|
||
Copyright DataStax, 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. | ||
|
||
--> | ||
<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"> | ||
<parent> | ||
<artifactId>langstream-api-gateway-auth</artifactId> | ||
<groupId>ai.langstream</groupId> | ||
<version>0.0.16-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>langstream-jwt-gateway-admin-auth</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>ai.langstream</groupId> | ||
<artifactId>langstream-api</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>ai.langstream</groupId> | ||
<artifactId>langstream-auth-jwt</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this reads like a breaking change and we have stuff in production, please don't do it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no it's not, it's an alias and they both work