Skip to content
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

Fix: local deployment issues #72

Merged
merged 15 commits into from
May 16, 2024
Merged
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
256 changes: 133 additions & 123 deletions DEPENDENCIES_BACKEND

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# *******************************************************************************
# Copyright (c) 2024 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
# https://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.
#
# SPDX-License-Identifier: Apache-2.0
# ********************************************************************************

services:
carslen marked this conversation as resolved.
Show resolved Hide resolved
database:
image: 'postgres:16.2'
ports:
- '5432:5432'
environment:
- 'POSTGRES_USER=dcm'
- 'POSTGRES_DB=dcm'
- 'POSTGRES_PASSWORD=dcm'
volumes:
- mydb:/var/lib/postgresql/data
- ./dev/create_keycloak_db.sh:/docker-entrypoint-initdb.d/create_second_db.sh

keycloak:
healthcheck:
test: ["CMD", "curl", "-f", "http://0.0.0.0:8888/auth/"]
interval: 5s
timeout: 2s
retries: 15
pid: "host"
depends_on:
- database
volumes:
- keycloak-data:/opt/keycloak/data/
- ./dev/dcm_realm.json:/opt/keycloak/data/import/dcm_realm_local.json
environment:
KEYCLOAK_ADMIN: admin
KEYCLOAK_ADMIN_PASSWORD: admin
DB_VENDOR: postgres
DB_ADDR: database
DB_DATABASE: auth
DB_USER: root
DB_PASSWORD: password
image: quay.io/keycloak/keycloak:24.0.2
entrypoint: ["/opt/keycloak/bin/kc.sh", "start-dev", "--import-realm"]
ports:
- "28080:8080"

volumes:
mydb:
keycloak-data:
47 changes: 39 additions & 8 deletions demand-capacity-mgmt-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>3.1.5</version>
<version>3.2.5</version>
</dependency>

<dependency>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you are touching this file, can you please remove duplicate dependency in line 82 though you did not add it

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with b3a7dbc

Expand Down Expand Up @@ -83,10 +83,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down Expand Up @@ -140,6 +136,36 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

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

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>

<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-common</artifactId>
<version>2.5.0</version>
</dependency>

<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.5.0</version>
</dependency>

<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.2.21</version>
</dependency>
</dependencies>

<build>
Expand All @@ -165,9 +191,9 @@
<plugin>
<groupId>com.hubspot.maven.plugins</groupId>
<artifactId>prettier-maven-plugin</artifactId>
<version>0.13</version>
<version>0.22</version>
<configuration>
<prettierJavaVersion>1.0.2</prettierJavaVersion>
<prettierJavaVersion>2.6.0</prettierJavaVersion>
<printWidth>120</printWidth>
<tabWidth>4</tabWidth>
<ignoreConfigFile>true</ignoreConfigFile>
Expand Down Expand Up @@ -200,7 +226,7 @@
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>application.properties</include>
<include>application.yml</include>
</includes>
<targetPath>BOOT-INF/classes/</targetPath>
</resource>
Expand All @@ -216,6 +242,11 @@
</includes>
<targetPath>META-INF</targetPath>
</resource>
<!-- add content of resources folder -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
@ConfigurationPropertiesScan
public class DemandCapacityMgmtBackendApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* ******************************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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.
*
* SPDX-License-Identifier: Apache-2.0
* *******************************************************************************
*/

package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.appinfo;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "app.openapi")
public record InfoConfiguration(String name, String description, String version) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* ******************************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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.
*
* SPDX-License-Identifier: Apache-2.0
* *******************************************************************************
*/

package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.config.openapi;
carslen marked this conversation as resolved.
Show resolved Hide resolved

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.OAuthFlow;
import io.swagger.v3.oas.models.security.OAuthFlows;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import java.util.Collections;
import lombok.AllArgsConstructor;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.appinfo.InfoConfiguration;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.security.AppSecurityConfigProperties;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@AllArgsConstructor
public class OpenApiConfig {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tried to run app, project fails to initialize this class. Please check if you also see this or update recommended local set up.

I am using openJDK 17

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No we tested with JDK 17 only

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you briefly update document Local Development Install. I will try again.

Copy link
Contributor Author

@nitin-vavdiya nitin-vavdiya May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was one annotation missing on the spring boot main class

README is also updated

Fixed with b3a7dbc


private final AppSecurityConfigProperties properties;
private final InfoConfiguration appInfoConfiguration;

@Bean
public OpenAPI customOpenAPI() {
Info info = new Info();
info.setTitle(appInfoConfiguration.name());
info.setDescription(appInfoConfiguration.description());
info.setVersion(appInfoConfiguration.version());
OpenAPI openAPI = new OpenAPI();
if (properties.enabled()) {
openAPI = enableSecurity(openAPI);
}
return openAPI.info(info);
}

@Bean
public GroupedOpenApi openApiDefinition() {
return GroupedOpenApi.builder().group("docs").pathsToMatch("/**").displayName("Docs").build();
}

private OpenAPI enableSecurity(OpenAPI openAPI) {
Components components = new Components();

//Auth using access_token
String accessTokenAuth = "Authenticate using access_token";
components.addSecuritySchemes(
accessTokenAuth,
new SecurityScheme()
.name(accessTokenAuth)
.description("Authenticate using token")
.type(SecurityScheme.Type.HTTP)
.scheme("Bearer")
);

//Auth using Resource Owner Password Flow
String passwordFlow = "Authenticate using Resource Owner Password Flow";
components.addSecuritySchemes(
passwordFlow,
new SecurityScheme()
.name(passwordFlow)
.description(
"Authenticate using Resource Owner Password Flow. provide username and password to authenticate"
)
.type(SecurityScheme.Type.OAUTH2)
.flows(
new OAuthFlows()
.password(
new OAuthFlow()
.tokenUrl(properties.tokenUrl())
.refreshUrl(properties.refreshTokenUrl())
)
)
);

return openAPI
.components(components)
.addSecurityItem(
new SecurityRequirement()
.addList(accessTokenAuth, Collections.emptyList())
.addList(passwordFlow, Collections.emptyList())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* ******************************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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.
*
* SPDX-License-Identifier: Apache-2.0
* *******************************************************************************
*/

package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.security;
carslen marked this conversation as resolved.
Show resolved Hide resolved

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("app.openapi.security")
public record AppSecurityConfigProperties(
Boolean enabled,
String authUrl,
String tokenUrl,
String refreshTokenUrl
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
"/token/introspect"
)
.permitAll()
.requestMatchers(HttpMethod.GET, "/docs/api-docs/**", "/ui/swagger-ui/**")
.permitAll()
.anyRequest()
.authenticated()
);
Expand Down
36 changes: 29 additions & 7 deletions demand-capacity-mgmt-backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@
# SPDX-License-Identifier: Apache-2.0
# ********************************************************************************

app:
openapi:
name: Demand and Capacity Management
description: Demand and Capacity Management project
version: 1
security:
enabled: ${SECURITY_ENABLE:true}
auth-url: ${keycloak.baseUrl}/realms/${keycloak.realm}/protocol/openid-connect/auth
nitin-vavdiya marked this conversation as resolved.
Show resolved Hide resolved
token-url: ${keycloak.baseUrl}/realms/${keycloak.realm}/protocol/openid-connect/token
refresh-token-url: ${KEYCLOAK_TOKEN_URL}

spring:
security:
oauth2:
resource-server:
jwt:
issuer-uri: http://localhost:8888/realms/dcm
issuer-uri: ${keycloak.baseUrl}/realms/${keycloak.realm}

datasource:
url: jdbc:postgresql://${DCM_DATASOURCE_HOST:localhost:5432}/${DCM_DATASOURCE_NAME:dcm}
Expand All @@ -40,13 +51,24 @@ spring:
user: ${DCM_DATASOURCE_USER:dcm}
password: ${DCM_DATASOURCE_PASS:dcm}
schemas: migrations
locations: classpath:db/migration/postgresql
locations: classpath:db/migration

keycloak:
baseUrl: http://localhost:8888
realm: dcm
clientId: dcmauth
clientSecret: ${DCMSECR}
baseUrl: ${KEYCLOAK_BASE_URL:http://localhost:28080}
realm: ${KEYCLOAK_REALM_NAME:dcm}
clientId: ${KEYCLOAK_CLIENT_ID:dcmauth}
clientSecret: ${DCMSECR:test}
grant_type_token: password
grant_type_refresh_token : refresh_token

springdoc:
swagger-ui:
oauth:
clientId: ${keycloak.clientId}
nitin-vavdiya marked this conversation as resolved.
Show resolved Hide resolved
disable-swagger-default-url: true
path: /ui/swagger-ui
show-common-extensions: true
csrf:
enabled: true
api-docs:
path: /docs/api-docs
Loading
Loading