Skip to content
This repository was archived by the owner on Sep 5, 2023. It is now read-only.

Commit 879acf1

Browse files
Sita04busunkim96partheagcf-owl-bot[bot]
authored
docs(samples): add reCAPTCHA Enterprise code samples (#112)
* docs(samples): added samples and tests for site key CRUD operation * docs(samples): added samples and tests for create assessment * docs(samples): modified requirements and test inputs * docs(samples): lint fix - adding copyright * fix(samples): added nox config and modified requirements-test * docs(samples): lint fix * refactor(samples): updated nox file * docs(samples): lint fix * docs(samples): added nox config * docs(samples): lint fix; modified nox config * refactor(samples): incorporated review comments * refactor(samples): lint fix * refactor(samples): lint fix * refactor(samples): lint fix * docs(samples): updated to use latest chrome version and lint fix * fix(samples): updated selenium to use chrome browser and added display name to update key * build: add placeholder dockerfile, update kokoro configs * Adding chrome installation in docker * refactor(samples): removed webdriver manager dependency. Included commands to install binary file in docker. * fix(samples): changed the chromedriver version to latest release. * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * refactor(samples): included review comments Co-authored-by: Bu Sun Kim <[email protected]> Co-authored-by: Bu Sun Kim <[email protected]> Co-authored-by: Anthonios Partheniou <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent ed4fbc7 commit 879acf1

30 files changed

+1119
-13
lines changed

.kokoro/docker/samples/Dockerfile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Use the python-docs-samples iamge as a base:
16+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/.kokoro/docker/Dockerfile
17+
FROM gcr.io/cloud-devrel-kokoro-resources/python-samples-testing-docker
18+
19+
# Install Chrome.
20+
RUN curl https://dl-ssl.google.com/linux/linux_signing_key.pub -o /tmp/google.pub \
21+
&& cat /tmp/google.pub | apt-key add -; rm /tmp/google.pub \
22+
&& echo 'deb http://dl.google.com/linux/chrome/deb/ stable main' > /etc/apt/sources.list.d/google.list \
23+
&& mkdir -p /usr/share/desktop-directories \
24+
&& apt-get -y update && apt-get install -y google-chrome-stable
25+
26+
# Disable the SUID sandbox so that Chrome can launch without being in a privileged container.
27+
RUN dpkg-divert --add --rename --divert /opt/google/chrome/google-chrome.real /opt/google/chrome/google-chrome \
28+
&& echo "#!/bin/bash\nexec /opt/google/chrome/google-chrome.real --no-sandbox --disable-setuid-sandbox \"\$@\"" > /opt/google/chrome/google-chrome \
29+
&& chmod 755 /opt/google/chrome/google-chrome
30+
31+
# Install Chrome Driver.
32+
RUN mkdir -p /opt/selenium \
33+
&& curl http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip -o /opt/selenium/chromedriver_linux64.zip \
34+
&& cd /opt/selenium; unzip /opt/selenium/chromedriver_linux64.zip; rm -rf chromedriver_linux64.zip; ln -fs /opt/selenium/chromedriver /usr/local/bin/chromedriver;
35+
36+
CMD ["python3.6"]

.kokoro/samples/lint/common.cfg

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ env_vars: {
1818
value: "github/python-recaptcha-enterprise/.kokoro/test-samples.sh"
1919
}
2020

21-
# Configure the docker image for kokoro-trampoline.
2221
env_vars: {
2322
key: "TRAMPOLINE_IMAGE"
24-
value: "gcr.io/cloud-devrel-kokoro-resources/python-samples-testing-docker"
23+
value: "gcr.io/cloud-devrel-kokoro-resources/python-recaptcha-enterprise-samples-docker"
24+
}
25+
26+
# Configure the docker image for kokoro-trampoline.
27+
env_vars: {
28+
key: "TRAMPOLINE_DOCKERFILE"
29+
value: ".kokoro/docker/samples/Dockerfile"
2530
}
2631

2732
# Download secrets for samples

.kokoro/samples/python3.10/common.cfg

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ env_vars: {
2424
value: "github/python-recaptcha-enterprise/.kokoro/test-samples.sh"
2525
}
2626

27-
# Configure the docker image for kokoro-trampoline.
2827
env_vars: {
2928
key: "TRAMPOLINE_IMAGE"
30-
value: "gcr.io/cloud-devrel-kokoro-resources/python-samples-testing-docker"
29+
value: "gcr.io/cloud-devrel-kokoro-resources/python-recaptcha-enterprise-samples-docker"
30+
}
31+
32+
# Configure the docker image for kokoro-trampoline.
33+
env_vars: {
34+
key: "TRAMPOLINE_DOCKERFILE"
35+
value: ".kokoro/docker/samples/Dockerfile"
3136
}
3237

3338
# Download secrets for samples

.kokoro/samples/python3.10/periodic-head.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ env_vars: {
99
key: "TRAMPOLINE_BUILD_FILE"
1010
value: "github/python-recaptcha-enterprise/.kokoro/test-samples-against-head.sh"
1111
}
12+
13+
# Upload the docker image after successful builds.
14+
env_vars: {
15+
key: "TRAMPOLINE_IMAGE_UPLOAD"
16+
value: "true"
17+
}

.kokoro/samples/python3.10/periodic.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ env_vars: {
44
key: "INSTALL_LIBRARY_FROM_SOURCE"
55
value: "False"
66
}
7+
8+
# Upload the docker image after successful builds.
9+
env_vars: {
10+
key: "TRAMPOLINE_IMAGE_UPLOAD"
11+
value: "true"
12+
}

.kokoro/samples/python3.6/common.cfg

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ env_vars: {
2424
value: "github/python-recaptcha-enterprise/.kokoro/test-samples.sh"
2525
}
2626

27-
# Configure the docker image for kokoro-trampoline.
2827
env_vars: {
2928
key: "TRAMPOLINE_IMAGE"
30-
value: "gcr.io/cloud-devrel-kokoro-resources/python-samples-testing-docker"
29+
value: "gcr.io/cloud-devrel-kokoro-resources/python-recaptcha-enterprise-samples-docker"
30+
}
31+
32+
# Configure the docker image for kokoro-trampoline.
33+
env_vars: {
34+
key: "TRAMPOLINE_DOCKERFILE"
35+
value: ".kokoro/docker/samples/Dockerfile"
3136
}
3237

3338
# Download secrets for samples

.kokoro/samples/python3.6/periodic-head.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ env_vars: {
99
key: "TRAMPOLINE_BUILD_FILE"
1010
value: "github/python-recaptcha-enterprise/.kokoro/test-samples-against-head.sh"
1111
}
12+
13+
# Upload the docker image after successful builds.
14+
env_vars: {
15+
key: "TRAMPOLINE_IMAGE_UPLOAD"
16+
value: "true"
17+
}

.kokoro/samples/python3.6/periodic.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ env_vars: {
44
key: "INSTALL_LIBRARY_FROM_SOURCE"
55
value: "False"
66
}
7+
8+
# Upload the docker image after successful builds.
9+
env_vars: {
10+
key: "TRAMPOLINE_IMAGE_UPLOAD"
11+
value: "true"
12+
}

.kokoro/samples/python3.7/common.cfg

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ env_vars: {
2424
value: "github/python-recaptcha-enterprise/.kokoro/test-samples.sh"
2525
}
2626

27-
# Configure the docker image for kokoro-trampoline.
2827
env_vars: {
2928
key: "TRAMPOLINE_IMAGE"
30-
value: "gcr.io/cloud-devrel-kokoro-resources/python-samples-testing-docker"
29+
value: "gcr.io/cloud-devrel-kokoro-resources/python-recaptcha-enterprise-samples-docker"
30+
}
31+
32+
# Configure the docker image for kokoro-trampoline.
33+
env_vars: {
34+
key: "TRAMPOLINE_DOCKERFILE"
35+
value: ".kokoro/docker/samples/Dockerfile"
3136
}
3237

3338
# Download secrets for samples

.kokoro/samples/python3.7/periodic.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ env_vars: {
44
key: "INSTALL_LIBRARY_FROM_SOURCE"
55
value: "False"
66
}
7+
8+
# Upload the docker image after successful builds.
9+
env_vars: {
10+
key: "TRAMPOLINE_IMAGE_UPLOAD"
11+
value: "true"
12+
}

.kokoro/samples/python3.8/common.cfg

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ env_vars: {
2424
value: "github/python-recaptcha-enterprise/.kokoro/test-samples.sh"
2525
}
2626

27-
# Configure the docker image for kokoro-trampoline.
2827
env_vars: {
2928
key: "TRAMPOLINE_IMAGE"
30-
value: "gcr.io/cloud-devrel-kokoro-resources/python-samples-testing-docker"
29+
value: "gcr.io/cloud-devrel-kokoro-resources/python-recaptcha-enterprise-samples-docker"
30+
}
31+
32+
# Configure the docker image for kokoro-trampoline.
33+
env_vars: {
34+
key: "TRAMPOLINE_DOCKERFILE"
35+
value: ".kokoro/docker/samples/Dockerfile"
3136
}
3237

3338
# Download secrets for samples

.kokoro/samples/python3.8/periodic-head.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ env_vars: {
99
key: "TRAMPOLINE_BUILD_FILE"
1010
value: "github/python-recaptcha-enterprise/.kokoro/test-samples-against-head.sh"
1111
}
12+
13+
# Upload the docker image after successful builds.
14+
env_vars: {
15+
key: "TRAMPOLINE_IMAGE_UPLOAD"
16+
value: "true"
17+
}

.kokoro/samples/python3.8/periodic.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ env_vars: {
44
key: "INSTALL_LIBRARY_FROM_SOURCE"
55
value: "False"
66
}
7+
8+
# Upload the docker image after successful builds.
9+
env_vars: {
10+
key: "TRAMPOLINE_IMAGE_UPLOAD"
11+
value: "true"
12+
}

.kokoro/samples/python3.9/common.cfg

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ env_vars: {
2424
value: "github/python-recaptcha-enterprise/.kokoro/test-samples.sh"
2525
}
2626

27-
# Configure the docker image for kokoro-trampoline.
2827
env_vars: {
2928
key: "TRAMPOLINE_IMAGE"
30-
value: "gcr.io/cloud-devrel-kokoro-resources/python-samples-testing-docker"
29+
value: "gcr.io/cloud-devrel-kokoro-resources/python-recaptcha-enterprise-samples-docker"
30+
}
31+
32+
# Configure the docker image for kokoro-trampoline.
33+
env_vars: {
34+
key: "TRAMPOLINE_DOCKERFILE"
35+
value: ".kokoro/docker/samples/Dockerfile"
3136
}
3237

3338
# Download secrets for samples

.kokoro/samples/python3.9/periodic-head.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ env_vars: {
99
key: "TRAMPOLINE_BUILD_FILE"
1010
value: "github/python-recaptcha-enterprise/.kokoro/test-samples-against-head.sh"
1111
}
12+
13+
# Upload the docker image after successful builds.
14+
env_vars: {
15+
key: "TRAMPOLINE_IMAGE_UPLOAD"
16+
value: "true"
17+
}

.kokoro/samples/python3.9/periodic.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ env_vars: {
44
key: "INSTALL_LIBRARY_FROM_SOURCE"
55
value: "False"
66
}
7+
8+
# Upload the docker image after successful builds.
9+
env_vars: {
10+
key: "TRAMPOLINE_IMAGE_UPLOAD"
11+
value: "true"
12+
}

owlbot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
# ----------------------------------------------------------------------------
4040
# Add templated files
4141
# ----------------------------------------------------------------------------
42-
templated_files = common.py_library(cov_level=98, microgenerator=True)
42+
templated_files = common.py_library(cov_level=98, microgenerator=True, custom_samples_dockerfile=True)
4343

4444
s.move(
4545
templated_files, excludes=[".coveragerc"]

samples/snippets/create_assessment.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Copyright 2021 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START recaptcha_enterprise_create_assessment]
16+
from google.cloud import recaptchaenterprise_v1
17+
18+
19+
def create_assessment(
20+
project_id: str, recaptcha_site_key: str, token: str, recaptcha_action: str
21+
) -> None:
22+
""" Create an assessment to analyze the risk of a UI action.
23+
Args:
24+
project_id: GCloud Project ID
25+
recaptcha_site_key: Site key obtained by registering a domain/app to use recaptcha services.
26+
token: The token obtained from the client on passing the recaptchaSiteKey.
27+
recaptcha_action: Action name corresponding to the token.
28+
"""
29+
30+
# TODO(developer): Replace these variables before running the sample.
31+
# Specify a name for this assessment.
32+
assessment_name = "assessment_name"
33+
34+
client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()
35+
36+
# Set the properties of the event to be tracked.
37+
event = recaptchaenterprise_v1.Event()
38+
event.site_key = recaptcha_site_key
39+
event.token = token
40+
41+
assessment = recaptchaenterprise_v1.Assessment()
42+
assessment.event = event
43+
assessment.name = assessment_name
44+
45+
project_name = f"projects/{project_id}"
46+
47+
# Build the assessment request.
48+
request = recaptchaenterprise_v1.CreateAssessmentRequest()
49+
request.assessment = assessment
50+
request.parent = project_name
51+
52+
response = client.create_assessment(request)
53+
54+
# Check if the token is valid.
55+
if not response.token_properties.valid:
56+
print(
57+
"The CreateAssessment call failed because the token was "
58+
+ "invalid for for the following reasons: "
59+
+ str(response.token_properties.invalid_reason)
60+
)
61+
return
62+
63+
# Check if the expected action was executed.
64+
if response.token_properties.action != recaptcha_action:
65+
print(
66+
"The action attribute in your reCAPTCHA tag does"
67+
+ "not match the action you are expecting to score"
68+
)
69+
return
70+
else:
71+
# Get the risk score and the reason(s)
72+
# For more information on interpreting the assessment,
73+
# see: https://cloud.google.com/recaptcha-enterprise/docs/interpret-assessment
74+
for reason in response.risk_analysis.reasons:
75+
print(reason)
76+
print(
77+
"The reCAPTCHA score for this token is: "
78+
+ str(response.risk_analysis.score)
79+
)
80+
81+
82+
# [END recaptcha_enterprise_create_assessment]

samples/snippets/create_site_key.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright 2021 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START recaptcha_enterprise_create_site_key]
16+
from google.cloud import recaptchaenterprise_v1
17+
18+
19+
def create_site_key(project_id: str, domain_name: str) -> str:
20+
"""Create reCAPTCHA Site key which binds a domain name to a unique key.
21+
Args:
22+
project_id : GCloud Project ID.
23+
domain_name: Specify the domain name in which the reCAPTCHA should be activated.
24+
"""
25+
client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()
26+
27+
# Set the type of the reCAPTCHA to be displayed.
28+
# For different types, see: https://cloud.google.com/recaptcha-enterprise/docs/keys
29+
web_settings = recaptchaenterprise_v1.WebKeySettings()
30+
web_settings.allowed_domains.append(domain_name)
31+
web_settings.allow_amp_traffic = False
32+
web_settings.integration_type = web_settings.IntegrationType.SCORE
33+
34+
key = recaptchaenterprise_v1.Key()
35+
key.display_name = "any descriptive name for the key"
36+
key.web_settings = web_settings
37+
38+
# Create the request.
39+
request = recaptchaenterprise_v1.CreateKeyRequest()
40+
request.parent = f"projects/{project_id}"
41+
request.key = key
42+
43+
# Get the name of the created reCAPTCHA site key.
44+
response = client.create_key(request)
45+
recaptcha_site_key = response.name.rsplit("/", maxsplit=1)[1]
46+
print("reCAPTCHA Site key created successfully. Site Key: " + recaptcha_site_key)
47+
return recaptcha_site_key
48+
49+
50+
# [END recaptcha_enterprise_create_site_key]
51+
52+
if __name__ == "__main__":
53+
import google.auth
54+
import google.auth.exceptions
55+
56+
# TODO(developer): Replace the below variables before running
57+
try:
58+
default_project_id = google.auth.default()[1]
59+
domain_name = "localhost"
60+
except google.auth.exceptions.DefaultCredentialsError:
61+
print(
62+
"Please use `gcloud auth application-default login` "
63+
"or set GOOGLE_APPLICATION_CREDENTIALS to use this script."
64+
)
65+
else:
66+
create_site_key(default_project_id, domain_name)

0 commit comments

Comments
 (0)