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

Commit 8862a82

Browse files
jbolingerfrankyn
authored andcommitted
Add DLP client sample (#72)
* add dlp client sample * add copy task for CI * update api activation link * added missing link
1 parent 5d4505f commit 8862a82

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1856
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/libraries
5+
/.idea/caches/
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
.DS_Store
9+
/build
10+
/captures
11+
.externalNativeBuild
12+
app/src/main/res/raw/credential.json
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>android-java-client-samples</name>
4+
<comment>Project android-java-client-samples created by Buildship.</comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
connection.project.dir=
2+
eclipse.preferences.version=1
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Cloud Data Loss Prevention API Example
2+
3+
This directory contains an Android example that uses the
4+
[Cloud Data Loss Prevention (DLP) API](https://cloud.google.com/dlp/)
5+
with the [Google Cloud Client Library for Java]
6+
7+
## Prerequisites
8+
9+
### Enable the Data Loss Prevention API
10+
11+
If you have not already done so,
12+
[enable the DLP API for your project](https://console.cloud.google.com/flows/enableapi?apiid=dlp.googleapis.com).
13+
14+
### Set Up to Authenticate With Your Project's Credentials
15+
16+
This Android app uses a JSON credential file locally stored in the raw resources folder.
17+
***You should not do this in your production app.***
18+
Instead, you should set up your own backend server that authenticates app users.
19+
The server should delegate API calls from your client app and enforce usage quota per user.
20+
Alternatively, you should get an access token on the server side,
21+
and supply them to your client app. Tokens expire after a short amount of time. For example:
22+
23+
```java
24+
// read JSON credential into stream and generate a credential using an access token
25+
GoogleCredentials credentials = GoogleCredentials.fromStream(stream).createScoped(SCOPE);
26+
AccessToken token = credentials.refreshAccessToken();
27+
return new GoogleCredentials(accessToken);
28+
```
29+
30+
In this example, we put the Service Account in the client so no backend server is needed
31+
to run it.
32+
33+
In order to try out this sample, visit the [Cloud Console](https://console.cloud.google.com/), and
34+
navigate to:
35+
`API Manager > Credentials > Create credentials > Service account key > New service account`.
36+
Create a new service account, and download the JSON credentials file. Put the file in the app
37+
resources as `app/src/main/res/raw/credential.json`.
38+
39+
Again, ***you should not do this in your production app.***
40+
41+
See the [Cloud Platform Auth Guide](https://cloud.google.com/docs/authentication#developer_workflow)
42+
for more information.
43+
44+
## Client Libraries on Android
45+
46+
The [Google Cloud Client Library for Java] that is used for this example can be used on Android,
47+
but it is not optimized and is considered experimental. Specifically, it does not use the
48+
[lite](https://github.com/google/protobuf/blob/master/java/lite.md) version of protocol buffers
49+
that are optimized for mobile, it has some dependencies that may not be needed on Android, and
50+
it does not help with the authentication issues mentioned the previous section. We are working
51+
on this and encourage you to try out the client libraries on Android and give us feedback so
52+
that we can improve them.
53+
54+
## Run the Example
55+
56+
Open this example and Android studio and run the app. This example uses the device's camera
57+
so if you are using an emulator it is recommended to set a webcam as the camera
58+
(you can do this when you create a new device with the AVD manager).
59+
60+
[Google Cloud Client Library for Java]: https://github.com/GoogleCloudPlatform/google-cloud-java
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
4+
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
5+
<classpathentry kind="output" path="bin/default"/>
6+
</classpath>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>app</name>
4+
<comment>Project app created by Buildship.</comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
connection.project.dir=..
2+
eclipse.preferences.version=1
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
apply plugin: 'com.android.application'
18+
19+
android {
20+
compileSdkVersion 27
21+
defaultConfig {
22+
applicationId "com.google.cloud.examples.dlp"
23+
minSdkVersion 24
24+
targetSdkVersion 27
25+
versionCode 1
26+
versionName "1.0"
27+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
28+
javaCompileOptions {
29+
annotationProcessorOptions {
30+
includeCompileClasspath false
31+
}
32+
}
33+
}
34+
buildTypes {
35+
release {
36+
minifyEnabled false
37+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
38+
}
39+
}
40+
dataBinding {
41+
enabled = true
42+
}
43+
compileOptions {
44+
targetCompatibility 1.8
45+
sourceCompatibility 1.8
46+
}
47+
// exclude files that are not needed from the cloud client libraries
48+
packagingOptions {
49+
exclude 'META-INF/DEPENDENCIES'
50+
exclude 'META-INF/LICENSE'
51+
exclude 'META-INF/LICENSE.txt'
52+
exclude 'META-INF/license.txt'
53+
exclude 'META-INF/NOTICE'
54+
exclude 'META-INF/NOTICE.txt'
55+
exclude 'META-INF/notice.txt'
56+
exclude 'META-INF/ASL2.0'
57+
exclude 'META-INF/INDEX.LIST'
58+
}
59+
}
60+
61+
dependencies {
62+
implementation fileTree(dir: 'libs', include: ['*.jar'])
63+
implementation 'com.android.support:appcompat-v7:27.1.1'
64+
65+
// include the DLP client library
66+
implementation 'io.grpc:grpc-okhttp:1.10.0'
67+
implementation 'com.google.cloud:google-cloud-dlp:0.50.0-beta'
68+
69+
testImplementation 'junit:junit:4.12'
70+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
71+
androidTestImplementation 'com.android.support.test:rules:1.0.2'
72+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
73+
}
74+
75+
task copySecretKey(type: Copy) {
76+
File secretKey = file "$System.env.GOOGLE_APPLICATION_CREDENTIALS"
77+
from secretKey.getParent()
78+
include secretKey.getName()
79+
into 'src/main/res/raw'
80+
rename secretKey.getName(), "credential.json"
81+
}
82+
preBuild.dependsOn(copySecretKey)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
22+
-keep class android.arch.** { *; }

0 commit comments

Comments
 (0)