10BC0 for #962 updated resync session name based on the reloaded dn · TremoloSecurity/OpenUnison@65edb48 · GitHub
[go: up one dir, main page]

Skip to content

Commit 65edb48

Browse files
committed
for #962 updated resync session name based on the reloaded dn
1 parent df672a8 commit 65edb48

File tree

6 files changed

+374
-35
lines changed

6 files changed

+374
-35
lines changed

unison/unison-applications-k8s/src/main/java/com/tremolosecurity/k8s/watch/K8sWatcher.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ public K8sWatcher(String k8sTarget,String namespace, String plural, String group
8484
public void initalRun() throws ProvisioningException {
8585

8686
OpenShiftTarget k8s = (OpenShiftTarget) provisioningEngine.getTarget(k8sTarget).getProvider();
87-
87+
88+
89+
8890
if (k8s == null) {
8991
throw new ProvisioningException("Target " + k8sTarget + " does not exist");
9092
}
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/*******************************************************************************
2+
* Copyright 2021 Tremolo Security, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*******************************************************************************/
16+
package com.tremolosecurity.proxy.dynamicconfiguration;
17+
18+
import com.tremolosecurity.config.util.ConfigManager;
19+
import com.tremolosecurity.config.xml.TremoloType;
20+
import com.tremolosecurity.k8s.watch.K8sWatchTarget;
21+
import com.tremolosecurity.k8s.watch.K8sWatcher;
22+
import com.tremolosecurity.openunison.util.config.OpenUnisonConfigLoader;
23+
import com.tremolosecurity.provisioning.core.ProvisioningException;
24+
import com.tremolosecurity.proxy.filters.SetupGroupMetadataWatch;
25+
import org.json.simple.JSONArray;
26+
import org.json.simple.JSONObject;
27+
import org.json.simple.parser.JSONParser;
28+
import org.json.simple.parser.ParseException;
29+
30+
public class LoadNamespaceGroupMetadataFromK8s implements K8sWatchTarget {
31+
static org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager.getLogger(LoadNamespaceGroupMetadataFromK8s.class.getName());
32+
33+
K8sWatcher k8sWatch;
34+
35+
TremoloType tremolo;
36+
37+
38+
private ConfigManager cfgMgr;
39+
40+
private SetupGroupMetadataWatch md;
41+
42+
43+
44+
45+
46+
47+
48+
49+
public void loadNamespaceGroupMetadatas(ConfigManager cfgMgr, String k8sTarget, String namespace,SetupGroupMetadataWatch md) throws ProvisioningException {
50+
this.tremolo = cfgMgr.getCfg();
51+
52+
53+
this.md = md;
54+
55+
this.cfgMgr = cfgMgr;
56+
57+
this.k8sWatch = new K8sWatcher(k8sTarget,namespace,"namespacegroupmetadatas","openunison.tremolo.io",this,cfgMgr,cfgMgr.getProvisioningEngine());
58+
59+
this.k8sWatch.initalRun();
60+
61+
}
62+
63+
64+
65+
@Override
66+
public void addObject(TremoloType cfg, JSONObject item) throws ProvisioningException {
67+
String rawJson = item.toJSONString();
68+
StringBuffer b = new StringBuffer();
69+
b.setLength(0);
70+
OpenUnisonConfigLoader.integrateIncludes(b,rawJson);
71+
try {
72+
JSONObject newRoot = (JSONObject) new JSONParser().parse(b.toString());
73+
JSONObject metadata = (JSONObject) newRoot.get("metadata");
74+
75+
if (metadata == null) {
76+
throw new ProvisioningException("No metadata");
77+
}
78+
79+
String name = (String) metadata.get("name");
80+
81+
logger.info("Adding NamespaceGroupMetadata " + name);
82+
83+
84+
try {
85+
JSONObject spec = (JSONObject) newRoot.get("spec");
86+
87+
this.md.addNamespaceMapping(name, spec);
88+
89+
90+
91+
92+
} catch (Exception e) {
93+
logger.warn("Could not initialize namespace group mapping " + name,e);
94+
return;
95+
}
96+
97+
98+
99+
100+
} catch (ParseException e) {
101+
throw new ProvisioningException("Could not parse custom namespacegroupmetadata",e);
102+
}
103+
104+
}
105+
106+
107+
108+
@Override
109+
public void modifyObject(TremoloType cfg, JSONObject item) throws ProvisioningException {
110+
String rawJson = item.toJSONString();
111+
StringBuffer b = new StringBuffer();
112+
b.setLength(0);
113+
OpenUnisonConfigLoader.integrateIncludes(b,rawJson);
114+
try {
115+
JSONObject newRoot = (JSONObject) new JSONParser().parse(b.toString());
116+
JSONObject metadata = (JSONObject) newRoot.get("metadata");
117+
118+
if (metadata == null) {
119+
throw new ProvisioningException("No metadata");
120+
}
121+
122+
String name = (String) metadata.get("name");
123+
124+
logger.info("Modifying NamespaceGroupMetadata " + name);
125+
126+
127+
try {
128+
JSONObject spec = (JSONObject) newRoot.get("spec");
129+
130+
String ext = (String) spec.get("externalName");
131+
132+
133+
134+
135+
136+
this.md.deleteNamespaceMapping(name);
137+
this.md.addNamespaceMapping(name,spec);
138+
139+
} catch (Exception e) {
140+
logger.warn("Could not initialize namespace group mapping " + name,e);
141+
return;
142+
}
143+
144+
145+
146+
147+
} catch (ParseException e) {
148+
throw new ProvisioningException("Could not parse namespacegroupmetadata",e);
149+
}
150+
151+
}
152+
153+
154+
155+
@Override
156+
public void deleteObject(TremoloType cfg, JSONObject item) throws ProvisioningException {
157+
String rawJson = item.toJSONString();
158+
StringBuffer b = new StringBuffer();
159+
b.setLength(0);
160+
OpenUnisonConfigLoader.integrateIncludes(b,rawJson);
161+
try {
162+
JSONObject newRoot = (JSONObject) new JSONParser().parse(b.toString());
163+
JSONObject metadata = (JSONObject) newRoot.get("metadata");
164+
165+
if (metadata == null) {
166+
throw new ProvisioningException("No metadata");
167+
}
168+
169+
String name = (String) metadata.get("name");
170+
171+
logger.info("Deleting NamespaceGroupMetadata " + name);
172+
173+
174+
try {
175+
JSONObject spec = (JSONObject) newRoot.get("spec");
176+
String k8s = (String) spec.get("groupName");
177+
String ext = (String) spec.get("externalName");
178+
179+
180+
181+
this.md.deleteNamespaceMapping(name);
182+
183+
} catch (Exception e) {
184+
logger.warn("Could not delete namespace group mapping " + name,e);
185+
return;
186+
}
187+
188+
189+
190+
191+
} catch (ParseException e) {
192+
throw new ProvisioningException("Could not parse groupmetadata",e);
193+
}
194+
195+
196+
}
197+
198+
199+
}

0 commit comments

Comments
 (0)
0