Missing Licenses
[aaf/authz.git] / auth / sample / cass_data / scrub.sh
1 #!/bin/bash
2 #########
3 #  ============LICENSE_START====================================================
4 #  org.onap.aaf
5 #  ===========================================================================
6 #  Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
7 #  ===========================================================================
8 #  Licensed under the Apache License, Version 2.0 (the "License");
9 #  you may not use this file except in compliance with the License.
10 #  You may obtain a copy of the License at
11 #
12 #       http://www.apache.org/licenses/LICENSE-2.0
13 #
14 #  Unless required by applicable law or agreed to in writing, software
15 #  distributed under the License is distributed on an "AS IS" BASIS,
16 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 #  See the License for the specific language governing permissions and
18 #  limitations under the License.
19 #  ============LICENSE_END====================================================
20 #
21
22 # Copies of Repo data need to be added to "dats" dir for loading by push.sh
23 #
24 # Further, repo data has dates that are out of date and may have IDs not valid.  We need to make
25 # sure the important data is consistent with Identities.
26 #
27 # This is expected to be run from a controlling Machine data "authz/auth/sample/cass_data" when a backup 
28 # is created that is intended to be "Bootstrap Data"
29 #
30 # Date resets on key data load on the system at load time
31 #
32
33 DIR=$(pwd)
34 ID_FILE=../data/sample.identities.dat
35
36 if [ -e $ID_FILE ]; then
37   if [ "$(uname -s)" = "Darwin" ]; then 
38     DATE=$(date -v "+6m" "+%Y-%m-%d %H:%M:%S.000+0000")
39   else 
40     DATE=$(date "+%Y-%m-%d %H:%M:%S.000+0000" -d "+6 months")
41   fi
42   echo $DATE
43
44   #### CRED
45   # Enter for People
46   CRED="cred.dat"
47   rm cred.dat
48   echo "Create default Passwords for all Identities in $CRED"
49   for ID in $(grep '|a|' $ID_FILE | sed -e "s/|.*//"); do
50      if [ "$ID" = "aaf" ]; then
51         DOMAIN="aaf.osaaf.org";
52      else
53         DOMAIN="$ID.onap.org";
54      fi
55      unset FIRST
56      for D in ${DOMAIN//./ }; do
57         if [ -z "$FIRST" ]; then
58           NS="$D"
59           FIRST="N"
60         else
61           NS="$D.$NS"
62         fi
63      done     
64      echo "$ID@$DOMAIN|2|${DATE}|0xd993c5617486296f1b99d04de31633332b8ba1a550038e23860f9dbf0b2fcf95|Initial ID|$NS|53344|" >> $CRED
65   done
66
67   for ID in $(grep '|e|' $ID_FILE | sed -e "s/|.*//"); do
68      echo "$ID@people.osaaf.org|2|${DATE}|0xd993c5617486296f1b99d04de31633332b8ba1a550038e23860f9dbf0b2fcf95|Initial ID|org.osaaf.people|53344|" >> $CRED
69   done
70
71   ##### USER_ROLE
72   echo "Scrubbing user_roles not in $ID_FILE"
73
74   ## Covering for when scrubbing in cass_init versus a Backup
75   if [ -d "dats" ]; then
76     for D in ns ns_attrib perm role config artifact; do 
77       if [ -e "dats/$D.dat" ]; then
78          cp dats/$D.dat .
79       fi
80     done
81   else
82     mkdir -p dats
83     cp user_role.dat dats
84     REMOVE_DATS=true
85   fi  
86   > user_role.dat
87   for ID in $(grep -v "#" $ID_FILE | awk -F\| '{print $1}' | grep -v "^$"); do
88       grep "$ID@" dats/user_role.dat >> user_role.dat
89   done
90
91   UR="$(mktemp)"
92   DUR="$(mktemp)"
93
94   cat user_role.dat | awk -F\| '{print $1}' | sort -u > $UR
95   cat dats/user_role.dat | awk -F\| '{print $1}' | sort -u > $DUR
96
97   echo "Removed IDs from user_roles"
98   diff $UR $DUR | grep "^>" | sort -u 
99
100   rm "$UR" "$DUR"
101   
102   mv user_role.dat tmp
103   sed "s/\(^.*|\)\(.*|\)\(.*|\)\(.*\)/\1${DATE}|\3\4/" tmp > user_role.dat 
104   rm tmp
105   if [ -n "$REMOVE_DATS" ]; then
106      rm -Rf dats
107   fi
108 else
109     echo $0 requires access to $ID_FILE
110 fi
111
112