<commit_before>#/bin/bash

#
# Copyright 2021 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
#
#     https://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.
#

# Credentials settings helper function
to_eval=""
function set_cred(){
    to_eval=$1
    local suffix=$2
    local apigee_user_cred="-u $APIGEE_USER -p $APIGEE_PASSWORD -o $APIGEE_ORG -e $APIGEE_ENV"
    local apigee_token_cred="-t $APIGEE_TOKEN -o $APIGEE_ORG -e $APIGEE_ENV"
    if [[ -z "${APIGEE_TOKEN}" ]]; then
        to_eval="${to_eval} ${apigee_user_cred}"
    else
        to_eval="${to_eval} ${apigee_token_cred}"
    fi
    to_eval="${to_eval} ${suffix}"
    eval "$to_eval"
}

# Create Caches and dynamic KVM used by oidc proxy
echo "--->"  Creating cache OIDCState...
set_cred "apigeetool createcache" "-z OIDCState --description \"Holds state during authorization_code flow\" --cacheExpiryInSecs 600"
echo "--->"  Creating cache PushedAuthReqs...
set_cred "apigeetool createcache" "-z PushedAuthReqs --description \"Holds Pushed Authorisation Requests during authorization_code_flow\" --cacheExpiryInSecs 600"
# echo "--->"  Creating dynamic KVM PPIDs...
# apigeetool createKVMmap -u $APIGEE_USER -p $APIGEE_PASSWORD -o $APIGEE_ORG -e $APIGEE_ENV --mapName PPIDs --encrypted
echo "--->"  Creating dynamic KVM TokensIssuedForConsent...
set_cred "apigeetool createKVMmap" "--mapName TokensIssuedForConsent --encrypted"
# Create KVM that will hold consent information
echo "--->"  Creating dynamic KVM Consents...
set_cred "apigeetool createKVMmap" "--mapName Consents --encrypted"
# Create cache that will hold consent state (Used by basic consent management proxy)
echo "--->"  Creating cache ConsentState...
set_cred "apigeetool createcache" "-z ConsentState --description \"Holds state during consent flow\" --cacheExpiryInSecs 600"


# KVM CDSConfig

# KVM mockOBBRClient

 # Deploy Shared flows
cd ../src/shared-flows
for sf in $(ls .) 
do 
    echo "--->"  Deploying $sf Shared Flow 
    cd $sf
    set_cred "apigeetool deploySharedflow" "-n $sf"
    cd ..
 done

 # Deploy banking apiproxies
cd ../apiproxies/banking
for ap in $(ls .) 
do 
    echo "--->"  Deploying $ap Apiproxy
    cd $ap
    set_cred "apigeetool deployproxy" "-n $ap"
    cd ..
 done

 # Deploy Common Proxies
cd ../common
for ap in $(ls .) 
do 
    echo "--->"  Deploying $ap Apiproxy
    cd $ap
    set_cred "apigeetool deployproxy" "-n $ap"
    cd ..
 done

# Deploy Admin Proxies
cd ../admin/OBBR-Admin
echo "--->"  Deploying OBBR-Admin Apiproxy
set_cred "apigeetool deployproxy" "-n OBBR-Admin"
cd ..

# Deploy utils Proxies
cd ../utils/mock-obbr-client
echo "--->"  Deploying mock-obbr-client Apiproxy
set_cred "apigeetool deployproxy" "-n mock-obbr-client"
cd ..

# Deploy authnz related Proxies
cd ../authnz
for ap in $(ls .) 
do 
    echo "--->"  Deploying $ap Apiproxy
    cd $ap
    set_cred "apigeetool deployproxy" "-n $ap"
    cd ..
 done

# Create products


# Create Dev


# Create app

# Revert to original directory
cd ../../../..<commit_msg>Test support / env config<commit_after>#/bin/bash

#
# Copyright 2021 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
#
#     https://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.
#

# Credentials settings helper function
to_eval=""
function set_cred(){
    to_eval=$1
    local suffix=$2
    local apigee_user_cred="-u $APIGEE_USER -p $APIGEE_PASSWORD -o $APIGEE_ORG -e $APIGEE_ENV"
    local apigee_token_cred="-t $APIGEE_TOKEN -o $APIGEE_ORG -e $APIGEE_ENV"
    if [[ -z "${APIGEE_TOKEN}" ]]; then
        to_eval="${to_eval} ${apigee_user_cred}"
    else
        to_eval="${to_eval} ${apigee_token_cred}"
    fi
    to_eval="${to_eval} ${suffix}"
    eval "$to_eval"
}

#### Utility functions
function replace_with_jwks_uri {
 POLICY_FILE=$1
 JWKS_PATH_SUFFIX=$2
 POLICY_BEFORE_JWKS_ELEM=$(sed  '/<JWKS/,$d' $POLICY_FILE)
 POLICY_AFTER_JWKS_ELEM=$(sed  '1,/<JWKS/d' $POLICY_FILE)
 echo $POLICY_BEFORE_JWKS_ELEM'<JWKS uri="https://'$APIGEE_ORG-$APIGEE_ENV'.apigee.net'$JWKS_PATH_SUFFIX'" />'$POLICY_AFTER_JWKS_ELEM > temp.xml
 # The following step is for pretty printing the resulting edited xml, we don't care if it fails. If failed, just use the original file
 xmllint --format temp.xml 1> temp2.xml 2> /dev/null
 if [ $? -eq 0 ]; then
    cp temp2.xml $POLICY_FILE
 else
    cp temp.xml $POLICY_FILE
 fi
 rm temp.xml temp2.xml 
}
# This function generates RSA Private/public key pair, and the corresponding JWKS file
function generate_private_public_key_pair {
   KEY_PAIR_NAME=$1
   KEY_PAIR_FRIENDLY_NAME=$2
   # Generate RSA Private/public key pair
   echo "--->"  "Generating RSA Private/public key pair for "$KEY_PAIR_FRIENDLY_NAME"..."
   
   OUT_FILE=$KEY_PAIR_NAME"_rsa_private.pem"
   openssl genpkey -algorithm RSA -out $OUT_FILE -pkeyopt rsa_keygen_bits:2048
   IN_FILE=$OUT_FILE
   OUT_FILE=$KEY_PAIR_NAME"_rsa_public.pem"
   openssl rsa -in $IN_FILE -pubout -out $OUT_FILE
   echo "Private/public key pair generated and stored in ./setup/certs. Please keep private key safe"
   echo "----"
   # Generate jwk format for public key (and store it in a file too) - Add missing attributes in jwk generated by command line
   IN_FILE=$OUT_FILE
   APP_JWK=$(pem-jwk $IN_FILE  | jq '{"keys": [. + { "kid": "PlaceHolderKid" } + { "use": "sig" }]}')  
   echo $APP_JWK > $KEY_PAIR_NAME.jwks
   sed  -i '' "s/PlaceHolderKid/$KEY_PAIR_NAME/" $KEY_PAIR_NAME.jwks
}

# Create Caches and dynamic KVM used by oidc proxy
echo "--->"  Creating cache OIDCState...
set_cred "apigeetool createcache" "-z OIDCState --description \"Holds state during authorization_code flow\" --cacheExpiryInSecs 600"
echo "--->"  Creating cache PushedAuthReqs...
set_cred "apigeetool createcache" "-z PushedAuthReqs --description \"Holds Pushed Authorisation Requests during authorization_code_flow\" --cacheExpiryInSecs 600"
# echo "--->"  Creating dynamic KVM PPIDs...
# apigeetool createKVMmap -u $APIGEE_USER -p $APIGEE_PASSWORD -o $APIGEE_ORG -e $APIGEE_ENV --mapName PPIDs --encrypted
echo "--->"  Creating dynamic KVM TokensIssuedForConsent...
set_cred "apigeetool createKVMmap" "--mapName TokensIssuedForConsent --encrypted"
# Create KVM that will hold consent information
echo "--->"  Creating dynamic KVM Consents...
set_cred "apigeetool createKVMmap" "--mapName Consents --encrypted"
# Create cache that will hold consent state (Used by basic consent management proxy)
echo "--->"  Creating cache ConsentState...
set_cred "apigeetool createcache" "-z ConsentState --description \"Holds state during consent flow\" --cacheExpiryInSecs 600"


# KVM ConsentsConfig

# Create KVM that will hold consent information
echo "--->"  Creating dynamic KVM CDSConfig...
set_cred "apigeetool createKVMmap" "--mapName Consents --encrypted"

# Create cache that will hold consent state (Used by basic consent management proxy)
echo "--->"  Creating cache ConsentState...
set_cred "apigeetool createcache" "-z ConsentState --description \"Holds state during consent flow\" --cacheExpiryInSecs 600"


# KVM mockOBBRClient

 # Deploy Shared flows
cd ../src/shared-flows
for sf in $(ls .) 
do 
    echo "--->"  Deploying $sf Shared Flow 
    cd $sf
    set_cred "apigeetool deploySharedflow" "-n $sf"
    cd ..
 done

 # Deploy banking apiproxies
cd ../apiproxies/banking
for ap in $(ls .) 
do 
    echo "--->"  Deploying $ap Apiproxy
    cd $ap
    set_cred "apigeetool deployproxy" "-n $ap"
    cd ..
 done

 # Deploy Common Proxies
cd ../common
for ap in $(ls .) 
do 
    echo "--->"  Deploying $ap Apiproxy
    cd $ap
    set_cred "apigeetool deployproxy" "-n $ap"
    cd ..
 done

# Deploy Admin Proxies
cd ../admin/OBBR-Admin
echo "--->"  Deploying OBBR-Admin Apiproxy
set_cred "apigeetool deployproxy" "-n OBBR-Admin"
cd ..

# Deploy utils Proxies
cd ../utils/mock-obbr-client
echo "--->"  Deploying mock-obbr-client Apiproxy
set_cred "apigeetool deployproxy" "-n mock-obbr-client"
cd ..

# Deploy authnz related Proxies
cd ../authnz
for ap in $(ls .) 
do 
    echo "--->"  Deploying $ap Apiproxy
    cd $ap
    set_cred "apigeetool deployproxy" "-n $ap"
    cd ..
 done

# Create products

echo "--->"  Creating API Product: "Accounts"
set_cred "apigeetool createProduct" "--productName \"CDSAccounts\" --displayName \"Accounts\" --approvalType \"auto\" --productDesc \"Get access to Accounts APIs\" --proxies CDS-Accounts --scopes \"bank:accounts.basic:read,bank:accounts.detail:read\""

echo "--->"  Creating API Product: "Transactions"
set_cred "apigeetool createProduct" "--productName \"CDSTransactions\" --displayName \"Transactions\" --approvalType \"auto\" --productDesc \"Get access to Transactions APIs\" --proxies CDS-Transactions --scopes \"bank:transactions:read\"" 

echo "--->"  Creating API Product: "OIDC"
set_cred "apigeetool createProduct" "--productName \"CDSOIDC\" --displayName \"OIDC\" --approvalType \"auto\" --productDesc \"Get access to authentication and authorisation requests\" --proxies oidc --scopes \"openid, profile\""

# Create product for dynamic client registration
echo "--->"  Creating API Product: "DynamicClientRegistration"
set_cred "apigeetool createProduct" "--productName \"CDSDynamicClientRegistration\" --displayName \"DynamicClientRegistration\" --approvalType \"auto\" --productDesc \"Dynamically register a client\" --proxies CDS-DynamicClientRegistration --scopes \"cdr:registration\""

# Create product for Admin
echo "--->"  Creating API Product: "Admin"
set_cred "apigeetool createProduct" "--productName \"CDSAdmin\" --displayName \"Admin\" --approvalType \"auto\" --productDesc \"Get access to Admin APIs\" --proxies CDS-Admin --scopes \"admin:metadata:update,admin:metrics.basic:read\""


# Create Dev

# Create a test developer who will own the test app
# If no developer name has been set, use a default
if [ -z "$OBBR_TEST_DEVELOPER_EMAIL" ]; then  OBBR_TEST_DEVELOPER_EMAIL=<EMAIL>; fi;
echo "--->"  Creating Test Developer: $OBBR_TEST_DEVELOPER_EMAIL
set_cred "apigeetool createDeveloper" "--email $OBBR_TEST_DEVELOPER_EMAIL --firstName \"OBBR Test\" --lastName \"Developer\"  --userName $OBBR_TEST_DEVELOPER_EMAIL"


# Create app

# Create a test app - Store the client key and secret
echo "--->"  Creating Test App: OOBRTestApp...

APP_CREDENTIALS=$(set_cred "apigeetool createApp" "--name OBBRTestApp --apiProducts \"OBBRTransactions,OBBRAccounts,OBBROIDC\" --email $OBBR_TEST_DEVELOPER_EMAIL --json | jq .credentials[0]")
APP_KEY=$(echo $APP_CREDENTIALS | jq -r .consumerKey)
APP_SECRET=$(echo $APP_CREDENTIALS | jq -r .consumerSecret)

# Revert to original directory
cd ../../../..
{'fraction_non_alphanumeric': 0.12279167395487144, 'fraction_numerical': 0.004985132062270421, 'mean_word_length': 3.7625989171178675, 'pattern_counts': {'":': 3, '<': 7, '<?xml version=': 0, '>': 43, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, 'xml': 9}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7530344', 'n_tokens_mistral': 3986, 'n_tokens_neox': 3821, 'n_words': 1192}