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

feat(recaptcha): add mfa sample #8665

Closed
wants to merge 41 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
426d88f
feat(recaptcha): add mfa sample
Sita04 Sep 21, 2023
8c38c81
lint fix
Sita04 Sep 21, 2023
2fb725f
lint fix and change java version
Sita04 Sep 21, 2023
287b872
testing to check if tests run
Sita04 Sep 25, 2023
10839b8
Merge remote-tracking branch 'origin/main' into mfa-update
Sita04 Sep 25, 2023
253b7fd
revert change and rename files
Sita04 Sep 25, 2023
99bb8d3
update pom
Sita04 Sep 26, 2023
b1b9f92
update pom
Sita04 Sep 26, 2023
5df777c
update pom
Sita04 Sep 26, 2023
160f8a7
refactor package name
Sita04 Sep 26, 2023
cfc8ce3
fix issue with template resolution
Sita04 Oct 2, 2023
a372434
add dependencies
Sita04 Oct 2, 2023
f8573c3
lint fix
Sita04 Oct 2, 2023
83f08fd
fix package name
Sita04 Oct 2, 2023
3c55dab
lint fix
Sita04 Oct 2, 2023
d9871d8
lint fix
Sita04 Oct 2, 2023
77a2e96
lint fix
Sita04 Oct 17, 2023
3db24af
check if chrome is installed
Sita04 Oct 17, 2023
b17a89c
refactor chrome driver installation
Sita04 Oct 17, 2023
f4242a8
comments to check installation
Sita04 Oct 17, 2023
88a99a8
fix path
Sita04 Oct 17, 2023
84aff14
update build command
Sita04 Oct 17, 2023
c310949
update command
Sita04 Oct 17, 2023
f767fff
revert directory
Sita04 Oct 17, 2023
57ae9fa
remove export command
Sita04 Oct 18, 2023
635c3b4
update acc to review
Sita04 Oct 18, 2023
ca79ff1
add symlink to chrome binary
Sita04 Oct 18, 2023
b20b23b
Update pom.xml
Sita04 Nov 9, 2023
cc11200
Merge remote-tracking branch 'origin/main' into mfa-update
Sita04 Nov 9, 2023
c66efed
Merge remote-tracking branch 'origin/main' into mfa-update
Sita04 Nov 23, 2023
949a761
Update pom.xml
Sita04 Nov 23, 2023
cae757b
Merge remote-tracking branch 'origin/main' into mfa-update
Sita04 Nov 23, 2023
10d9b67
commenting out to test installation from docker image
Sita04 Nov 23, 2023
0219627
add password check helper
Sita04 Nov 23, 2023
35dbe66
add version
Sita04 Nov 23, 2023
b4abf20
Merge remote-tracking branch 'origin/mfa-update' into mfa-update
Sita04 Nov 23, 2023
e5c585d
update deps and wire SB class file
Sita04 Nov 23, 2023
66a9201
Merge remote-tracking branch 'origin' into mfa-update
Sita04 Nov 23, 2023
a07e37b
testing update with selenium version
Sita04 Nov 24, 2023
4b9bf54
uncomment and override chrome installation
Sita04 Nov 24, 2023
ea95ad2
uncomment and override chrome installation
Sita04 Nov 24, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions .kokoro/tests/build_recaptcha.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

# Copyright 2023 Google LLC.
#
# 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.

set -eo pipefail

current_dir="$PWD"
# Based on this content: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix
# https://github.com/alixaxel/chrome-aws-lambda/issues/164
echo "Y" | apt install libnss3
echo "Y" | apt install libnss3-dev libgdk-pixbuf2.0-dev libgtk-3-dev libxss-dev libgconf-2-4

# Install Chrome.
curl https://dl-ssl.google.com/linux/linux_signing_key.pub -o /tmp/google.pub \
&& cat /tmp/google.pub | apt-key add -; rm /tmp/google.pub \
&& echo 'deb http://dl.google.com/linux/chrome/deb/ stable main' > /etc/apt/sources.list.d/google.list \
&& mkdir -p /usr/share/desktop-directories \
&& apt-get -y update && apt-get install -y google-chrome-stable

# Disable the SUID sandbox so that Chrome can launch without being in a privileged container.
echo "Y" | dpkg-divert --add --rename --divert /opt/google/chrome/google-chrome.real /opt/google/chrome/google-chrome \
&& echo "#!/bin/bash\nexec /opt/google/chrome/google-chrome.real --no-sandbox --disable-setuid-sandbox \"\$@\"" > /opt/google/chrome/google-chrome \
&& chmod 755 /opt/google/chrome/google-chrome \
&& ln -fs /opt/google/chrome/google-chrome /usr/bin/google-chrome

# Install chrome driver.
echo "Y" | mkdir -p /opt/selenium \
&& curl http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip -o /opt/selenium/chromedriver_linux64.zip \
&& cd /opt/selenium; unzip /opt/selenium/chromedriver_linux64.zip; rm -rf chromedriver_linux64.zip; ln -fs /opt/selenium/chromedriver /usr/local/bin/chromedriver;

echo "Installed chrome and driver."

cd "$current_dir"
# Do not use exec to preserve trap behavior.
"$@"
14 changes: 14 additions & 0 deletions .kokoro/tests/run_test_java.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ if [[ "$file" == *"functions/helloworld/"* ]]; then
fi
fi

# Install Chrome and chrome driver for recaptcha tests
echo "Starting chrome driver installation: $file"
if [[ "$file" == *"recaptcha_enterprise/snippets"* ]]; then
source "$SCRIPT_DIR"/build_recaptcha.sh
EXIT=$?

if [[ $EXIT -ne 0 ]]; then
RTN=1
echo -e "\n Chrome driver installation failed. \n"
else
echo -e "\n Chrome driver started successfully. \n"
fi
fi

# Use maven to execute the tests for the project.
mvn --quiet --batch-mode --fail-at-end clean verify \
-Dfile.encoding="UTF-8" \
Expand Down
29 changes: 0 additions & 29 deletions .kokoro/tests/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,35 +118,6 @@ if [[ ",$JAVA_VERSION," =~ "11" ]]; then
cd ../../
fi

# Install Chrome and chrome driver for recaptcha tests
if [[ "$file" == *"recaptcha_enterprise/"* ]]; then

# Based on this content: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix
# https://github.com/alixaxel/chrome-aws-lambda/issues/164
apt install libnss3
apt install libnss3-dev libgdk-pixbuf2.0-dev libgtk-3-dev libxss-dev libgconf-2-4

# Install Chrome.
curl https://dl-ssl.google.com/linux/linux_signing_key.pub -o /tmp/google.pub \
&& cat /tmp/google.pub | apt-key add -; rm /tmp/google.pub \
&& echo 'deb http://dl.google.com/linux/chrome/deb/ stable main' > /etc/apt/sources.list.d/google.list \
&& mkdir -p /usr/share/desktop-directories \
&& apt-get -y update && apt-get install -y google-chrome-stable

# Disable the SUID sandbox so that Chrome can launch without being in a privileged container.
dpkg-divert --add --rename --divert /opt/google/chrome/google-chrome.real /opt/google/chrome/google-chrome \
&& echo "#!/bin/bash\nexec /opt/google/chrome/google-chrome.real --no-sandbox --disable-setuid-sandbox \"\$@\"" > /opt/google/chrome/google-chrome \
&& chmod 755 /opt/google/chrome/google-chrome

# Install chrome driver.
mkdir -p /opt/selenium \
&& curl http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip -o /opt/selenium/chromedriver_linux64.zip \
&& cd /opt/selenium; unzip /opt/selenium/chromedriver_linux64.zip; rm -rf chromedriver_linux64.zip; ln -fs /opt/selenium/chromedriver /usr/local/bin/chromedriver;

export CHROME_DRIVER_PATH="$PWD/chromedriver"
echo "Installing chrome and driver. Path to installation: $CHROME_DRIVER_PATH"
fi

btlr_args=(
"run"
"--max-cmd-duration=40m"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,38 @@
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-recaptchaenterprise</artifactId>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>recaptcha-password-check-helpers</artifactId>
<version>1.0.2</version>
</dependency>

<!-- [Start_Selenium_dependencies] -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.15.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>4.15.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-http-jdk-client</artifactId>
<version>4.13.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependency>
<!-- Removes the dependency of driver binary file -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package app;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RestController
@RequestMapping
public class MainController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,19 @@ public class AnnotateAssessment {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectID = "project-id";
String projectId = "project-id";
String assessmentId = "assessment-id";
annotateAssessment(projectID, assessmentId);
annotateAssessment(projectId, assessmentId);
}

/**
* Pre-requisite: Create an assessment before annotating.
* Annotate an assessment to provide feedback on the correctness of recaptcha prediction.
*
* <p>Annotate an assessment to provide feedback on the correctness of recaptcha prediction.
*
* @param projectID: GCloud Project id
* @param assessmentId: Value of the 'name' field returned from the CreateAssessment call.
* @param projectId Google Cloud Project ID.
* @param assessmentId Value of the 'name' field returned from the CreateAssessment call.
*/
public static void annotateAssessment(String projectID, String assessmentId) throws IOException {
public static void annotateAssessment(String projectId, String assessmentId) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the `client.close()` method on the client to safely
Expand All @@ -54,7 +53,7 @@ public static void annotateAssessment(String projectID, String assessmentId) thr
// https://cloud.google.com/recaptcha-enterprise/docs/annotate-assessment#when_to_annotate
AnnotateAssessmentRequest annotateAssessmentRequest =
AnnotateAssessmentRequest.newBuilder()
.setName(AssessmentName.of(projectID, assessmentId).toString())
.setName(AssessmentName.of(projectId, assessmentId).toString())
.setAnnotation(Annotation.FRAUDULENT)
.addReasons(Reason.FAILED_TWO_FACTOR)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,26 @@ public class CreateAssessment {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectID = "project-id";
String projectId = "project-id";
String recaptchaSiteKey = "recaptcha-site-key";
String token = "action-token";
String recaptchaAction = "action-name";

createAssessment(projectID, recaptchaSiteKey, token, recaptchaAction);
createAssessment(projectId, recaptchaSiteKey, token, recaptchaAction);
}

/**
* Create an assessment to analyze the risk of an UI action. Assessment approach is the same for
* both 'score' and 'checkbox' type recaptcha site keys.
*
* @param projectID : GCloud Project ID
* @param recaptchaSiteKey : Site key obtained by registering a domain/app to use recaptcha
* @param projectId Google Cloud Project ID.
* @param recaptchaSiteKey Site key obtained by registering a domain/app to use recaptcha
* services. (score/ checkbox type)
* @param token : The token obtained from the client on passing the recaptchaSiteKey.
* @param recaptchaAction : Action name corresponding to the token.
* @param token The token obtained from the client on passing the recaptchaSiteKey.
* @param recaptchaAction Action name corresponding to the token.
*/
public static void createAssessment(
String projectID, String recaptchaSiteKey, String token, String recaptchaAction)
String projectId, String recaptchaSiteKey, String token, String recaptchaAction)
throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
Expand All @@ -63,7 +63,7 @@ public static void createAssessment(
// Build the assessment request.
CreateAssessmentRequest createAssessmentRequest =
CreateAssessmentRequest.newBuilder()
.setParent(ProjectName.of(projectID).toString())
.setParent(ProjectName.of(projectId).toString())
.setAssessment(Assessment.newBuilder().setEvent(event).build())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ public class CreateSiteKey {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectID = "your-project-id";
String projectId = "your-project-id";
String domainName = "domain-name";

createSiteKey(projectID, domainName);
createSiteKey(projectId, domainName);
}

/**
* Create reCAPTCHA Site key which binds a domain name to a unique key.
*
* @param projectID : GCloud Project ID.
* @param domainName : Specify the domain name in which the reCAPTCHA should be activated.
* @param projectId Google Cloud Project ID.
* @param domainName Specify the domain name in which the reCAPTCHA should be activated.
*/
public static String createSiteKey(String projectID, String domainName) throws IOException {
public static String createSiteKey(String projectId, String domainName) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the `client.close()` method on the client to safely
Expand All @@ -64,7 +64,7 @@ public static String createSiteKey(String projectID, String domainName) throws I

CreateKeyRequest createKeyRequest =
CreateKeyRequest.newBuilder()
.setParent(ProjectName.of(projectID).toString())
.setParent(ProjectName.of(projectId).toString())
.setKey(scoreKey)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ public class DeleteSiteKey {
public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
String projectID = "your-project-id";
String projectId = "your-project-id";
String recaptchaSiteKey = "recaptcha-site-key";

deleteSiteKey(projectID, recaptchaSiteKey);
deleteSiteKey(projectId, recaptchaSiteKey);
}

/**
* Delete the given reCAPTCHA site key present under the project ID.
*
* @param projectID: GCloud Project ID.
* @param recaptchaSiteKey: Specify the site key to be deleted.
* @param projectId Google Cloud Project ID.
* @param recaptchaSiteKey Specify the site key to be deleted.
*/
public static void deleteSiteKey(String projectID, String recaptchaSiteKey)
public static void deleteSiteKey(String projectId, String recaptchaSiteKey)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
Expand All @@ -54,7 +54,7 @@ public static void deleteSiteKey(String projectID, String recaptchaSiteKey)
// Set the project ID and reCAPTCHA site key.
DeleteKeyRequest deleteKeyRequest =
DeleteKeyRequest.newBuilder()
.setName(KeyName.of(projectID, recaptchaSiteKey).toString())
.setName(KeyName.of(projectId, recaptchaSiteKey).toString())
.build();

client.deleteKeyCallable().futureCall(deleteKeyRequest).get(5, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public static void main(String[] args) throws IOException {
* Get metrics specific to a recaptcha site key. E.g: score bucket count for a key or number of
* times the checkbox key failed/ passed etc.,
*
* @param projectId: Google Cloud Project Id.
* @param recaptchaSiteKey: Specify the site key to get metrics.
* @param projectId Google Cloud Project ID.
* @param recaptchaSiteKey Specify the site key to get metrics.
*/
public static void getMetrics(String projectId, String recaptchaSiteKey) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ public class GetSiteKey {
public static void main(String[] args)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
String projectID = "your-project-id";
String projectId = "your-project-id";
String recaptchaSiteKey = "recaptcha-site-key";

getSiteKey(projectID, recaptchaSiteKey);
getSiteKey(projectId, recaptchaSiteKey);
}

/**
* Get the reCAPTCHA site key present under the project ID.
*
* @param projectID: GCloud Project ID.
* @param recaptchaSiteKey: Specify the site key to get the details.
* @param projectId Google Cloud Project ID.
* @param recaptchaSiteKey Specify the site key to get the details.
*/
public static void getSiteKey(String projectID, String recaptchaSiteKey)
public static void getSiteKey(String projectId, String recaptchaSiteKey)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
Expand All @@ -56,7 +56,7 @@ public static void getSiteKey(String projectID, String recaptchaSiteKey)
// Construct the "GetSiteKey" request.
GetKeyRequest getKeyRequest =
GetKeyRequest.newBuilder()
.setName(KeyName.of(projectID, recaptchaSiteKey).toString())
.setName(KeyName.of(projectId, recaptchaSiteKey).toString())
.build();

// Wait for the operation to complete.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ public class ListSiteKeys {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectID = "your-project-id";
String projectId = "your-project-id";

listSiteKeys(projectID);
listSiteKeys(projectId);
}

/**
* List all keys present under the given project ID.
*
* @param projectID : GCloud Project ID.
* @param projectId Google Cloud Project ID.
*/
public static ListKeysPagedResponse listSiteKeys(String projectID) throws IOException {
public static ListKeysPagedResponse listSiteKeys(String projectId) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the `client.close()` method on the client to safely
// clean up any remaining background resources.
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
// Set the project ID to list the keys present in it.
ListKeysRequest listKeysRequest =
ListKeysRequest.newBuilder().setParent(ProjectName.of(projectID).toString()).build();
ListKeysRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).build();

ListKeysPagedResponse response = client.listKeys(listKeysRequest);
System.out.println("Listing reCAPTCHA site keys: ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public static void main(String[] args) throws IOException {
* to reCAPTCHA Enterprise. For more info, see:
* https://cloud.google.com/recaptcha-enterprise/docs/migrate-recaptcha
*
* @param projectId: Google Cloud Project Id.
* @param recaptchaSiteKey: Specify the site key to migrate.
* @param projectId Google Cloud Project ID.
* @param recaptchaSiteKey Specify the site key to migrate.
*/
public static void migrateKey(String projectId, String recaptchaSiteKey) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
Expand Down