summaryrefslogtreecommitdiffstats
path: root/core/src/main/java/com/att/cadi/config/UsersDump.java
blob: 992b36534092cb9c3fe075d997a1574717b71635 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * 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
 * * 
 *  *      http://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.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package com.att.cadi.config;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Date;
import java.util.HashSet;

import com.att.cadi.AbsUserCache;
import com.att.cadi.lur.LocalLur;

public class UsersDump {

	/**
	 * @param args
	 */
	public static boolean write(OutputStream os, AbsUserCache<?> lur) {
		PrintStream ps;
		if(os instanceof PrintStream) {
			ps = (PrintStream)os;
		} else {
			ps = new PrintStream(os);
		}
		try {
			ps.println("<?xml version='1.0' encoding='utf-8'?>");
			ps.println("<!--");
			ps.print(  "     Code Generated Tomcat Users and Roles from AT&T LUR on ");
			ps.println(new Date());
			ps.println(  "-->");
			ps.println("<tomcat-users>");

			// We loop through Users, but want to write Groups first... therefore, save off print
			StringBuilder sb = new StringBuilder();
			
			// Obtain all unique role names
			HashSet<String> groups = new HashSet<String>();
			for(AbsUserCache<?>.DumpInfo di : lur.dumpInfo()) {
				sb.append("\n  <user username=\"");
				sb.append(di.user);
				sb.append("\" roles=\"");
				boolean first = true;
				for(String role : di.perms) {
					groups.add(role);
					if(first)first = false;
					else sb.append(',');
					sb.append(role);
				}
				sb.append("\"/>");

			}

			// Print roles
			for(String group : groups) {
				ps.print("  <role rolename=\"");
				ps.print(group);
				ps.println("\"/>");
			}
	
			ps.println(sb);

			ps.println("</tomcat-users>");
			ps.flush();
		} catch (Throwable t) {
			t.printStackTrace(ps);
			return false;
		}
		return true;
	}
	
	/**
	 * 
	 * Note: This method returns a String if there's an error, or null if ok.
	 * This unusual style is necessitated by the fact that any Exceptions thrown are likely to 
	 * be unlogged and hidden from view, making debugging almost impossible.
	 * 
	 * @param writeto
	 * @param up
	 * @return
	 */
	public static String updateUsers(String writeto, LocalLur up) {
		// Dump a Tomcat-user.xml lookalike (anywhere)
		if(writeto!=null) {
			// First read content
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			if(UsersDump.write(baos, up)) {
				byte[] postulate = baos.toByteArray();
				// now get contents of file
				File file = new File(writeto);
				boolean writeIt;
				if(file.exists()) {
					try {
						FileInputStream fis = new FileInputStream(file);
						byte[] orig = new byte[(int)file.length()];
						try {
							fis.read(orig);
						} finally {
							fis.close();
						}
						// Starting at third "<" (<tomcat-users> line)
						int startA=0, startB=0;
						for(int i=0;startA<orig.length && i<3;++startA) if(orig[startA]=='<')++i;
						for(int i=0;startB<orig.length && i<3;++startB) if(postulate[startB]=='<')++i;
						
						writeIt=orig.length-startA!=postulate.length-startB; // first, check if remaining length is the same
						while(!writeIt && startA<orig.length && startB<postulate.length) {
							if(orig[startA++]!=postulate[startB++])writeIt = true;
						}
					} catch (Exception e) {
						writeIt = true;
					}
				} else {
					writeIt = true;
				}
				
				if(writeIt) {
					try {
						FileOutputStream fos = new FileOutputStream(file);
						try {
							fos.write(postulate);
						} finally {
							fos.close();
						}
					} catch (IOException e) {
						return e.getMessage();
					}
				}
			}
		}
		return null; // no message means ok.
	}

}