84161b50bddc73cefb4fc484147b15b3a2279417
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / configure / PlaceArtifactScripts.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.configure;
23
24 import java.io.File;
25
26 import org.onap.aaf.cadi.CadiException;
27 import org.onap.aaf.cadi.util.Chmod;
28 import org.onap.aaf.misc.env.Trans;
29 import org.onap.aaf.misc.env.util.Chrono;
30 import org.onap.aaf.misc.env.util.Split;
31
32 import certman.v1_0.Artifacts.Artifact;
33 import certman.v1_0.CertInfo;
34
35 public class PlaceArtifactScripts extends ArtifactDir {
36         @Override
37         public boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException {
38                 try {
39                         // Setup check.sh script
40                         String filename = arti.getNs()+".check.sh";
41                         File f1 = new File(dir,filename);
42                         String email = arti.getNotification() + '\n';
43                         if(email.startsWith("mailto:")) {
44                                 email=email.substring(7);
45                         }  else {
46                                 email=arti.getOsUser() + '\n';
47                         }
48                         
49                         StringBuilder classpath = new StringBuilder();
50                         boolean first = true;
51                         for(String pth : Split.split(File.pathSeparatorChar, System.getProperty("java.class.path"))) {
52                                 if(first) {
53                                         first=false;
54                                 } else {
55                                         classpath.append(File.pathSeparatorChar);
56                                 }
57                                 File f = new File(pth);
58                                 classpath.append(f.getCanonicalPath().replaceAll("[0-9]+\\.[0-9]+\\.[0-9]+","*"));
59                         }
60                         
61                         write(f1,Chmod.to644,
62                                         "#!/bin/bash " + f1.getCanonicalPath()+'\n',
63                                         "# Certificate Manager Check Script\n",
64                                         "# Check on Certificate, and renew if needed.\n",
65                                         "# Generated by Certificate Manager " + Chrono.timeStamp()+'\n',
66                                         "DIR="+arti.getDir()+'\n',
67                                         "APP="+arti.getNs()+'\n',
68                                         "EMAIL="+email,
69                                         "CP=\""+classpath.toString()+"\"\n",
70                                         checkScript
71                                         );
72                         
73                         // Setup check.sh script
74                         File f2 = new File(dir,arti.getNs()+".crontab.sh");
75                         write(f2,Chmod.to644,
76                                         "#!/bin/bash " + f2.getCanonicalPath()+'\n',
77                                         "# Certificate Manager Crontab Loading Script\n",
78                                         "# Add/Update a Crontab entry, that adds a check on Certificate Manager generated Certificate nightly.\n",
79                                         "# Generated by Certificate Manager " + Chrono.timeStamp()+'\n',
80                                         "TFILE=\"/tmp/cmcron$$.temp\"\n",
81                                         "DIR=\""+arti.getDir()+"\"\n",
82                                         "CF=\""+arti.getNs()+" Certificate Check Script\"\n",
83                                         "SCRIPT=\""+f1.getCanonicalPath()+"\"\n",
84                                         cronScript
85                                         );
86
87                 } catch (Exception e) {
88                         throw new CadiException(e);
89                 }
90                 return true;
91         }
92         
93         /**
94          * Note: java.home gets Absolute Path of Java, where we probably want soft links from 
95          * JAVA_HOME
96          * @return
97          */
98         private final static String javaHome() {
99                 String rc = System.getenv("JAVA_HOME");
100                 return rc==null?System.getProperty("java.home"):rc;
101         }
102         private final static String checkScript = 
103                         "> $DIR/$APP.msg\n\n" +
104                         "function mailit {\n" +
105                         "  if [ -e /bin/mail ]; then\n" +
106                         "     MAILER=/bin/mail\n" +
107                         "  elif [ -e /usr/bin/mail ]; then \n" +
108                         "     MAILER=/usr/bin/mail\n" +
109                         "  else \n" +
110                         "     MAILER=\"\"\n" +
111                         "  fi\n" +
112                         " if [ \"$MAILER\" = \"\" ]; then\n" +
113                         "    printf \"$*\"\n" +
114                         " else \n" +
115                         "    printf \"$*\" | $MAILER -s \"AAF Certman Notification for `uname -n`\" $EMAIL\n"+
116                         " fi\n" +
117                         "}\n\n" +
118                         javaHome() + "/bin/" +"java -cp $CP " +
119                                 Agent.class.getName() + 
120                                 " cadi_prop_files=$DIR/$APP.props check 2>  $DIR/$APP.STDERR > $DIR/$APP.STDOUT\n" +
121                         "case \"$?\" in\n" +
122                         "  0)\n" +
123                         "    # Note: Validation will be mailed only the first day after any modification\n" +
124                         "    if [ \"`find $DIR -mtime 0 -name $APP.check.sh`\" != \"\" ] ; then\n" +
125                         "       mailit `echo \"Certficate Validated:\\n\\n\" | cat - $DIR/$APP.msg`\n" +
126                         "    else\n" +
127                         "       cat $DIR/$APP.msg\n" +
128                         "    fi\n" +
129                         "    ;;\n" +
130                         "  1) mailit \"Error with Certificate Check:\\\\n\\\\nCheck logs $DIR/$APP.STDOUT and $DIR/$APP.STDERR on `uname -n`\"\n" +
131                         "    ;;\n" +
132                         "  2) mailit `echo \"Certificate Check Error\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
133                         "    ;;\n" +
134                         "  10) mailit `echo \"Certificate Replaced\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
135                         "      if [ -e $DIR/$APP.restart.sh ]; then\n" +
136                         "        # Note: it is THIS SCRIPT'S RESPONSIBILITY to notify upon success or failure as necessary!!\n" +
137                         "        /bin/sh $DIR/$APP.restart.sh\n" +
138                         "      fi\n" +
139                         "    ;;\n" +
140                         "  *) mailit `echo \"Unknown Error code for CM Agent\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
141                         "    ;;\n" +
142                         " esac\n\n" +
143                         " # Note: make sure to cover this sripts' exit Code\n";
144         
145         private final static String cronScript = 
146                         "crontab -l | sed -n \"/#### BEGIN $CF/,/END $CF ####/!p\" > $TFILE\n" +
147                         "# Note: Randomize Minutes (0-60) and hours (1-4)\n" +
148                         "echo \"#### BEGIN $CF ####\" >> $TFILE\n" +
149                         "echo \"$(( $RANDOM % 60)) $(( $(( $RANDOM % 3 )) + 1 )) * * * /bin/bash $SCRIPT " +
150                                 ">> $DIR/cronlog 2>&1 \" >> $TFILE\n" +
151                         "echo \"#### END $CF ####\" >> $TFILE\n" +
152                         "crontab $TFILE\n" +
153                         "rm $TFILE\n";
154 }
155
156
157