Skip to content

Commit

Permalink
GRAD2-2620 (#523)
Browse files Browse the repository at this point in the history
* Added update-kc-client.sh

* Updated script, tested for missing, updated secret and client

* Added client secret to dc, etc.

* Added security config for new client

* Removed access token from files

* Fixing unit tests

* Removing access tokens

* Added unit testing for coverage.

* Cleaning up code smells.

---------

Co-authored-by: chris.ditcher <[email protected]>
  • Loading branch information
cditcher and chris.ditcher authored May 8, 2024
1 parent 8264cc0 commit 0ffb9c4
Show file tree
Hide file tree
Showing 23 changed files with 501 additions and 132 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/build.from.developer.branch.deploy.to.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ env:
MAX_MEM: "1500Mi"
MIN_REPLICAS: "3"
MAX_REPLICAS: "7"
SOAM_KC_REALM_ID: "master"

on:
# https://docs.github.com/en/actions/reference/events-that-trigger-workflows
Expand Down Expand Up @@ -145,6 +146,16 @@ jobs:
${{ env.BUSINESS_NAMESPACE }} \
${{ secrets.SPLUNK_TOKEN }} \
${{ vars.APP_LOG_LEVEL }}
# UPDATE KC Client
curl -s https://raw.githubusercontent.com/bcgov/${{ env.REPO_NAME }}/${{ env.BRANCH }}/tools/config/update-kc-client.sh \
| bash /dev/stdin \
dev \
${{ env.COMMON_NAMESPACE }} \
${{ env.SOAM_KC_REALM_ID }} \
${{ secrets.CLIENT_ID }} \
${{ env.BRANCH }} \
${{ env.REPO_NAME }}
# Start rollout (if necessary) and follow it
oc rollout latest dc/${{ env.SPRING_BOOT_IMAGE_NAME }} 2> /dev/null \
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/build.from.main.branch.deploy.to.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ env:
MAX_MEM: "1500Mi"
MIN_REPLICAS: "3"
MAX_REPLICAS: "7"
SOAM_KC_REALM_ID: "master"

on:
# https://docs.github.com/en/actions/reference/events-that-trigger-workflows
Expand Down Expand Up @@ -127,6 +128,16 @@ jobs:
${{ env.BUSINESS_NAMESPACE }} \
${{ secrets.SPLUNK_TOKEN }} \
${{ vars.APP_LOG_LEVEL }}
# UPDATE KC Client
curl -s https://raw.githubusercontent.com/bcgov/${{ env.REPO_NAME }}/${{ env.BRANCH }}/tools/config/update-kc-client.sh \
| bash /dev/stdin \
dev \
${{ env.COMMON_NAMESPACE }} \
${{ env.SOAM_KC_REALM_ID }} \
${{ secrets.CLIENT_ID }} \
${{ env.BRANCH }} \
${{ env.REPO_NAME }}
# Start rollout (if necessary) and follow it
oc rollout latest dc/${{ env.SPRING_BOOT_IMAGE_NAME }} 2> /dev/null \
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/build.from.release.branch.deploy.to.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ env:
MAX_MEM: "1500Mi"
MIN_REPLICAS: "3"
MAX_REPLICAS: "7"
SOAM_KC_REALM_ID: "master"

on:
# https://docs.github.com/en/actions/reference/events-that-trigger-workflows
Expand Down Expand Up @@ -134,6 +135,16 @@ jobs:
${{ env.BUSINESS_NAMESPACE }} \
${{ secrets.SPLUNK_TOKEN }} \
${{ vars.APP_LOG_LEVEL }}
# UPDATE KC Client
curl -s https://raw.githubusercontent.com/bcgov/${{ env.REPO_NAME }}/${{ env.BRANCH }}/tools/config/update-kc-client.sh \
| bash /dev/stdin \
dev \
${{ env.COMMON_NAMESPACE }} \
${{ env.SOAM_KC_REALM_ID }} \
${{ secrets.CLIENT_ID }} \
${{ env.BRANCH }} \
${{ env.REPO_NAME }}
# Start rollout (if necessary) and follow it
oc rollout latest dc/${{ env.SPRING_BOOT_IMAGE_NAME }} 2> /dev/null \
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/deploy_prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ env:
MAX_MEM: "1500Mi"
MIN_REPLICAS: "3"
MAX_REPLICAS: "7"
SOAM_KC_REALM_ID: "master"

on:
# https://docs.github.com/en/actions/reference/events-that-trigger-workflows
Expand Down Expand Up @@ -83,6 +84,16 @@ jobs:
${{ secrets.SPLUNK_TOKEN }} \
${{ vars.APP_LOG_LEVEL }}
# UPDATE KC Client
curl -s https://raw.githubusercontent.com/bcgov/${{ env.REPO_NAME }}/${{ env.BRANCH }}/tools/config/update-kc-client.sh \
| bash /dev/stdin \
dev \
${{ env.COMMON_NAMESPACE }} \
${{ env.SOAM_KC_REALM_ID }} \
${{ secrets.CLIENT_ID }} \
${{ env.BRANCH }} \
${{ env.REPO_NAME }}
# Start rollout (if necessary) and follow it
oc rollout latest dc/${{ env.SPRING_BOOT_IMAGE_NAME }} 2> /dev/null \
|| true && echo "Rollout in progress"
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/deploy_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ env:
MAX_MEM: "1500Mi"
MIN_REPLICAS: "3"
MAX_REPLICAS: "7"
SOAM_KC_REALM_ID: "master"

on:
# https://docs.github.com/en/actions/reference/events-that-trigger-workflows
Expand Down Expand Up @@ -83,6 +84,16 @@ jobs:
${{ secrets.SPLUNK_TOKEN }} \
${{ vars.APP_LOG_LEVEL }}
# UPDATE KC Client
curl -s https://raw.githubusercontent.com/bcgov/${{ env.REPO_NAME }}/${{ env.BRANCH }}/tools/config/update-kc-client.sh \
| bash /dev/stdin \
dev \
${{ env.COMMON_NAMESPACE }} \
${{ env.SOAM_KC_REALM_ID }} \
${{ secrets.CLIENT_ID }} \
${{ env.BRANCH }} \
${{ env.REPO_NAME }}
# Start rollout (if necessary) and follow it
oc rollout latest dc/${{ env.SPRING_BOOT_IMAGE_NAME }} 2> /dev/null \
|| true && echo "Rollout in progress"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
import org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
Expand All @@ -18,20 +25,48 @@
@Profile("!test")
public class RestWebClient {

@Autowired
EducGraduationApiConstants constants;

@Autowired
LogHelper logHelper;

private final HttpClient httpClient;
@Autowired
public RestWebClient(EducGraduationApiConstants constants, LogHelper logHelper) {
this.constants = constants;
this.logHelper = logHelper;
}

public RestWebClient() {
this.httpClient = HttpClient.create().compress(true)
.resolver(spec -> spec.queryTimeout(Duration.ofMillis(200)).trace("DNS", LogLevel.TRACE));
this.httpClient.warmup().block();
@Bean("graduationClient")
public WebClient getGraduationClientWebClient(OAuth2AuthorizedClientManager authorizedClientManager) {
ServletOAuth2AuthorizedClientExchangeFilterFunction filter = new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
filter.setDefaultClientRegistrationId("graduationclient");
return WebClient.builder()
.exchangeStrategies(ExchangeStrategies
.builder()
.codecs(codecs -> codecs
.defaultCodecs()
.maxInMemorySize(50 * 1024 * 1024))
.build())
.apply(filter.oauth2Configuration())
.filter(this.log())
.build();
}
@Bean
public OAuth2AuthorizedClientManager authorizedClientManager(
ClientRegistrationRepository clientRegistrationRepository,
OAuth2AuthorizedClientRepository authorizedClientRepository) {
OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
.clientCredentials()
.build();
DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager(
clientRegistrationRepository, authorizedClientRepository);
authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
return authorizedClientManager;
}

/**
* Old web client. You can use a @Qualifier('default') to summon it.
* @return
*/
@Bean
public WebClient webClient() {
return WebClient.builder().exchangeStrategies(ExchangeStrategies.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ public void restoreStudentGradStatus(String studentID, String accessToken,boolea

public List<GraduationStudentRecord> getStudentListByMinCode(String schoolOfRecord, String accessToken) {
List<Map> response = this.restService.get(String.format(educGraduationApiConstants.getGradStudentListSchoolReport(),schoolOfRecord),
List.class,
accessToken);
List.class);
return jsonTransformer.convertValue(response, new TypeReference<>(){});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,8 @@ public byte[] getSchoolReports(List<String> uniqueSchoolList, String type, Strin

public Integer createAndStoreSchoolReports(List<String> uniqueSchoolList, String type, String accessToken) {
int numberOfReports = 0;
Pair<String, Long> res = Pair.of(accessToken, System.currentTimeMillis());
ExceptionMessage exception = new ExceptionMessage();
int i = 0;
for (String usl : uniqueSchoolList) {
if (i == 0) {
res = getAccessToken(accessToken);
} else {
res = checkAndGetAccessToken(res);
}
accessToken = res.getLeft();

try {
List<GraduationStudentRecord> stdList = gradStatusService.getStudentListByMinCode(usl, accessToken);
if(logger.isDebugEnabled()) {
Expand All @@ -281,8 +272,6 @@ public Integer createAndStoreSchoolReports(List<String> uniqueSchoolList, String
List<Student> gradRegStudents = processStudentList(filterStudentList(stdList, GRADREG), type);
logger.debug("*** Process processGradRegReport {} for {} students", schoolObj.getMincode(), gradRegStudents.size());
numberOfReports = processGradRegReport(schoolObj, gradRegStudents, usl, accessToken, numberOfReports);
res = checkAndGetAccessToken(res);
accessToken = res.getLeft();
List<Student> nonGradRegStudents = processStudentList(filterStudentList(stdList, NONGRADREG), type);
logger.debug("*** Process processNonGradRegReport {} for {} students", schoolObj.getMincode(), nonGradRegStudents.size());
numberOfReports = processNonGradRegReport(schoolObj, nonGradRegStudents, usl, accessToken, numberOfReports);
Expand All @@ -292,7 +281,6 @@ public Integer createAndStoreSchoolReports(List<String> uniqueSchoolList, String
} catch (Exception e) {
logger.error("Failed to generate {} report for mincode: {} due to: {}", type, usl, e.getLocalizedMessage());
}
i++;
}
return numberOfReports;
}
Expand Down Expand Up @@ -423,8 +411,7 @@ private byte[] getSchoolReportGradRegReport(ReportData data, String mincode, Str

return this.restService.post(educGraduationApiConstants.getSchoolGraduation(),
reportParams,
byte[].class,
accessToken);
byte[].class);

}

Expand All @@ -437,16 +424,15 @@ private byte[] createAndSaveSchoolReportGradRegReport(ReportData data, String mi

SchoolReports requestObj = getSchoolReports(mincode, encodedPdf, GRADREG);

updateSchoolReport(accessToken, requestObj);
updateSchoolReport(requestObj);

return bytesSAR;
}

private void updateSchoolReport(String accessToken, SchoolReports requestObj) {
private void updateSchoolReport(SchoolReports requestObj) {
this.restService.post(educGraduationApiConstants.getUpdateSchoolReport(),
requestObj,
SchoolReports.class,
accessToken);
SchoolReports.class);
}

private String getEncodedPdfFromBytes(byte[] bytesSAR) {
Expand Down Expand Up @@ -474,7 +460,7 @@ private void createAndSaveSchoolReportNonGradRegReport(ReportData data, String m
byte[] bytesSAR = getSchoolReportNonGradRegReport(data, mincode, accessToken);
String encodedPdf = getEncodedPdfFromBytes(bytesSAR);
SchoolReports requestObj = getSchoolReports(mincode, encodedPdf, NONGRADREG);
updateSchoolReport(accessToken, requestObj);
updateSchoolReport(requestObj);
}

private byte[] getSchoolReportStudentNonGradPrjReport(ReportData data, String mincode, String accessToken) {
Expand All @@ -489,8 +475,7 @@ private byte[] getSchoolReportStudentNonGradPrjReport(ReportData data, String mi

return this.restService.post(educGraduationApiConstants.getStudentNonGradProjected(),
reportParams,
byte[].class,
accessToken);
byte[].class);
}

@Generated
Expand All @@ -514,7 +499,7 @@ private void createAndSaveSchoolReportStudentNonGradPrjReport(ReportData data, S
byte[] bytesSAR = getSchoolReportStudentNonGradPrjReport(data, mincode, accessToken);
String encodedPdf = getEncodedPdfFromBytes(bytesSAR);
SchoolReports requestObj = getSchoolReports(mincode, encodedPdf, NONGRADPRJ);
updateSchoolReport(accessToken, requestObj);
updateSchoolReport(requestObj);
}

/**
Expand Down
Loading

0 comments on commit 0ffb9c4

Please sign in to comment.