1
2
3
4
5
6
7
8
9
10
11 package gov.bnl.gums.hostToGroup;
12
13 import gov.bnl.gums.configuration.Configuration;
14
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.StringTokenizer;
19
20 import javax.persistence.Entity;
21 import javax.persistence.Transient;
22
23
24
25
26
27
28 @Entity
29 public class CertificateHostToGroupMapping extends HostToGroupMapping {
30 private List<String> regexs;
31
32
33 protected String cn;
34 protected String dn;
35
36
37
38
39 public CertificateHostToGroupMapping() {
40 super();
41 }
42
43
44
45
46
47
48 public CertificateHostToGroupMapping(Configuration configuration) {
49 super(configuration, null);
50 }
51
52
53
54
55
56
57 public String getCn() {
58 return this.cn;
59 }
60
61
62
63
64
65
66
67 public String getDn() {
68 return this.dn;
69 }
70
71 public boolean isInGroup(String hostname) {
72 Iterator<String> iter = regexs.iterator();
73 while (iter.hasNext()) {
74 if (hostname.matches((String) iter.next()))
75 return true;
76 }
77 return false;
78 }
79
80
81
82
83
84
85
86 @ConfigFieldAnnotation(label="CNs", example="*.host1.com, *.host2.com", help="only requests from matching hosts are accepted")
87 public void setCn(String cn) {
88 this.cn = cn;
89 updateRegEx();
90 if (cn!=null)
91 setName(cn);
92 }
93
94
95
96
97
98
99
100
101
102 @ConfigFieldAnnotation(label="DNs", example="*.host1.com, *.host2.com", help="only requests from matching host DNs are accepted")
103 public void setDn(String dn) {
104 this.dn = dn;
105 updateRegEx();
106 if (dn!=null)
107 setName(dn);
108 }
109
110 private void updateRegEx() {
111 regexs = new ArrayList<String>();
112 if (cn != null) {
113 StringTokenizer tokens = new StringTokenizer(cn, ",");
114 while (tokens.hasMoreTokens()) {
115 String regex = tokens.nextToken();
116 regex = regex.trim();
117 regex = regex.replaceAll("\\.", "\\.");
118 regex = regex.replaceAll("\\*", "[^\\./=]*");
119 regexs.add("(/[^=]*=[^=]*)*/CN=" + regex + "(/[^=]*=[^=]*)*");
120 }
121 }
122 if (dn != null) {
123 StringTokenizer tokens = new StringTokenizer(dn, ",");
124 while (tokens.hasMoreTokens()) {
125 String regex = tokens.nextToken();
126 regex = regex.trim();
127 regex = regex.replaceAll("\\.", "\\.");
128 regex = regex.replaceAll("\\*", "[^\\./=]*");
129 regexs.add(regex);
130 }
131 }
132 }
133 }