Skip to content

Commit a3aa6b5

Browse files
committed
Mod21 client lib tweaks
1 parent 0108eaf commit a3aa6b5

File tree

6 files changed

+43
-32
lines changed

6 files changed

+43
-32
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Memcache | [12 ⇒] 13 | Moving off App Engine `memcache` makes your apps mor
8484
Cloud Functions | 11 | Cloud Functions does not support Python 2, so after the Module 1 migration, you need to upgrade your app to Python 3 before attempting this migration, recommended if you have a very small App Engine app, or it has only one function/feature.
8585
Cloud Run | 4 or 5 | **Module 4** covers migrating to Cloud Run with Docker. Those unfamiliar with containers or do not wish to create/maintain a `Dockerfile` should do **Module 5**. Those doing **Module 4** will get additional information about Cloud Run in **Module 5** not covered in **Module 4**.
8686
Blobstore | [15 ⇒] 16 | Moving off App Engine `blobstore` makes your apps more portable, so the **Module 16** Cloud Storage migration is _recommended_ for those using `blobstore`. Those unfamiliar with `blobstore` should do **Module 15** first to add its usage to the sample app.
87-
Users | [20 ⇒] 21 | Moving off App Engine `users` makes your apps more portable, so the **Module 21** Cloud Storage migration is _recommended_ for those using `users`. Those unfamiliar with `users` should do **Module 20** first to add its usage to the sample app.
87+
Users | [20 ⇒] 21 | Moving off App Engine `users` makes your apps more portable, so the **Module 21** Cloud Identity Platform migration is _recommended_ for those using `users`. Those unfamiliar with `users` should do **Module 20** first to add its usage to the sample app.
8888
General migration | 6 ⇒ 10 ⇒ 14 | This series is more generic and not targeting a specific feature migration, but rather if you need to migrate your App Engine apps from one running project to another. It starts with **Module 6** if you need to migrate your code, say from Datastore to Firestore. **Module 10** is if you need to migrate your data from one project to another, and finally, **Module 14** is after you're done migrating your app, your data, or both, and need to migrate a running service on one GCP project to another.
8989

9090

mod21a-idenplat/main.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,30 @@
1818
from googleapiclient import discovery
1919
from firebase_admin import auth, initialize_app
2020

21+
# initialize Flask and Cloud NDB API client
22+
app = Flask(__name__)
23+
ds_client = ndb.Client()
24+
25+
2126
def _get_gae_admins():
2227
'return set of App Engine admins'
23-
# setup constants for calling Cloud IAM Resource Manager
28+
# setup constants for calling Cloud IAM Resource Manager API
2429
CREDS, PROJ_ID = default( # Application Default Credentials and project ID
2530
['https://www.googleapis.com/auth/cloud-platform'])
26-
IAM = discovery.build('cloudresourcemanager', 'v1', credentials=CREDS)
31+
rm_client = discovery.build('cloudresourcemanager', 'v1', credentials=CREDS)
2732
_TARGETS = frozenset(( # App Engine admin roles
2833
'roles/viewer',
2934
'roles/editor',
3035
'roles/owner',
3136
'roles/appengine.appAdmin',
3237
))
3338

34-
# collate all users who are members of at least one GAE admin role (TARGETS)
35-
admins = set() # set of all App Engine admins
36-
allow_policy = IAM.projects().getIamPolicy(resource=PROJ_ID).execute()
39+
# collate all users who are members of at least one GAE admin role (_TARGETS)
40+
admins = set() # set of all App Engine admins
41+
allow_policy = rm_client.projects().getIamPolicy(resource=PROJ_ID).execute()
3742
for b in allow_policy['bindings']: # bindings in IAM allow policy
3843
if b['role'] in _TARGETS: # only look at GAE admin roles
39-
admins.update(user.split(':', 1)[1] for user in b['members'])
44+
admins.update(user.split(':', 1).pop() for user in b['members'])
4045
return admins
4146

4247
@app.route('/is_admin', methods=['POST'])
@@ -47,10 +52,8 @@ def is_admin():
4752
return {'admin': email in _ADMINS}, 200
4853

4954

50-
# initialize Flask, Firebase, Cloud NDB; fetch set of App Engine admins
51-
app = Flask(__name__)
55+
# initialize Firebase and fetch set of App Engine admins
5256
initialize_app()
53-
ds_client = ndb.Client()
5457
_ADMINS = _get_gae_admins()
5558

5659

mod21a-idenplat/templates/index.html

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
signOut
1717
} from "https://www.gstatic.com/firebasejs/9.10.0/firebase-auth.js";
1818

19-
// Firebase config: at least 'apiKey' & 'authDomain' are required; go to
20-
// console.firebase.google.com/project/PROJECT_ID/settings/general/web OR
21-
// console.firebase.google.com/project/_/settings/general/web & pick project
19+
// Firebase config:
20+
// 1a. Go to: console.cloud.google.com/customer-identity/providers
21+
// 1b. May be prompted to enable GCIP and upgrade from Firebase
22+
// 2. Click: "Application Setup Details" button
23+
// 3. Copy: 'apiKey' and 'authDomain' from 'config' variable
2224
var firebaseConfig = {
2325
apiKey: "YOUR_API_KEY",
2426
authDomain: "YOUR_AUTH_DOMAIN",
@@ -28,6 +30,7 @@
2830
initializeApp(firebaseConfig);
2931
var auth = getAuth();
3032
var provider = new GoogleAuthProvider();
33+
//provider.setCustomParameters({prompt: 'select_account'});
3134

3235
// define login and logout button functions
3336
function login() {

mod21b-idenplat/main.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,33 @@
1414

1515
from flask import Flask, render_template, request
1616
from google.auth import default
17-
from google.cloud import ndb
18-
from googleapiclient import discovery
17+
from google.cloud import ndb, resourcemanager
1918
from firebase_admin import auth, initialize_app
2019

20+
# initialize Flask and Cloud NDB API client
21+
app = Flask(__name__)
22+
ds_client = ndb.Client()
23+
24+
2125
def _get_gae_admins():
2226
'return set of App Engine admins'
23-
# setup constants for calling Cloud IAM Resource Manager
24-
CREDS, PROJ_ID = default( # Application Default Credentials and project ID
25-
['https://www.googleapis.com/auth/cloud-platform'])
26-
IAM = discovery.build('cloudresourcemanager', 'v1', credentials=CREDS)
27+
# setup constants for calling Cloud IAM Resource Manager API
28+
_, PROJ_ID = default( # Application Default Credentials and project ID
29+
['https://www.googleapis.com/auth/cloudplatformprojects.readonly'])
30+
rm_client = resourcemanager.ProjectsClient()
2731
_TARGETS = frozenset(( # App Engine admin roles
2832
'roles/viewer',
2933
'roles/editor',
3034
'roles/owner',
3135
'roles/appengine.appAdmin',
3236
))
3337

34-
# collate all users who are members of at least one GAE admin role (TARGETS)
35-
admins = set() # set of all App Engine admins
36-
allow_policy = IAM.projects().getIamPolicy(resource=PROJ_ID).execute()
37-
for b in allow_policy['bindings']: # bindings in IAM allow policy
38-
if b['role'] in _TARGETS: # only look at GAE admin roles
39-
admins.update(user.split(':', 1)[1] for user in b['members'])
38+
# collate all users who are members of at least one GAE admin role (_TARGETS)
39+
admins = set() # set of all App Engine admins
40+
allow_policy = rm_client.get_iam_policy(resource='projects/%s' % PROJ_ID)
41+
for b in allow_policy.bindings: # bindings in IAM allow policy
42+
if b.role in _TARGETS: # only look at GAE admin roles
43+
admins.update(user.split(':', 1).pop() for user in b.members)
4044
return admins
4145

4246
@app.route('/is_admin', methods=['POST'])
@@ -47,10 +51,8 @@ def is_admin():
4751
return {'admin': email in _ADMINS}, 200
4852

4953

50-
# initialize Flask, Firebase, Cloud NDB; fetch set of App Engine admins
51-
app = Flask(__name__)
54+
# initialize Firebase and fetch set of App Engine admins
5255
initialize_app()
53-
ds_client = ndb.Client()
5456
_ADMINS = _get_gae_admins()
5557

5658

mod21b-idenplat/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
flask
2-
google-api-python-client
32
google-auth
43
google-cloud-ndb
4+
google-cloud-resource-manager
55
firebase-admin

mod21b-idenplat/templates/index.html

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
signOut
1717
} from "https://www.gstatic.com/firebasejs/9.10.0/firebase-auth.js";
1818

19-
// Firebase config: at least 'apiKey' & 'authDomain' are required; go to
20-
// console.firebase.google.com/project/PROJECT_ID/settings/general/web OR
21-
// console.firebase.google.com/project/_/settings/general/web & pick project
19+
// Firebase config:
20+
// 1a. Go to: console.cloud.google.com/customer-identity/providers
21+
// 1b. May be prompted to enable GCIP and upgrade from Firebase
22+
// 2. Click: "Application Setup Details" button
23+
// 3. Copy: 'apiKey' and 'authDomain' from 'config' variable
2224
var firebaseConfig = {
2325
apiKey: "YOUR_API_KEY",
2426
authDomain: "YOUR_AUTH_DOMAIN",
@@ -28,6 +30,7 @@
2830
initializeApp(firebaseConfig);
2931
var auth = getAuth();
3032
var provider = new GoogleAuthProvider();
33+
//provider.setCustomParameters({prompt: 'select_account'});
3134

3235
// define login and logout button functions
3336
function login() {

0 commit comments

Comments
 (0)