1919import java .util .concurrent .atomic .AtomicBoolean ;
2020import java .util .concurrent .atomic .AtomicReference ;
2121import com .jcraft .jsch .JSchException ;
22+ import io .sentrius .sso .automation .auditing .RuleFactory ;
2223import io .sentrius .sso .automation .sideeffects .SideEffect ;
2324import io .sentrius .sso .automation .sideeffects .SideEffectType ;
2425import io .sentrius .sso .core .config .SystemOptions ;
2829import io .sentrius .sso .core .model .dto .HostSystemDTO ;
2930import io .sentrius .sso .core .model .dto .UserTypeDTO ;
3031import io .sentrius .sso .core .model .hostgroup .HostGroup ;
32+ import io .sentrius .sso .core .model .hostgroup .ProfileRule ;
3133import io .sentrius .sso .core .model .security .UserType ;
3234import io .sentrius .sso .core .model .security .enums .ApplicationAccessEnum ;
3335import io .sentrius .sso .core .model .security .enums .AutomationAccessEnum ;
4345import io .sentrius .sso .core .repository .UserTypeRepository ;
4446import io .sentrius .sso .core .security .service .CryptoService ;
4547import io .sentrius .sso .core .services .HostGroupService ;
48+ import io .sentrius .sso .core .services .RuleService ;
4649import io .sentrius .sso .core .services .UserService ;
4750import io .sentrius .sso .install .configuration .InstallConfiguration ;
4851import io .sentrius .sso .install .configuration .dtos .HostGroupConfigurationDTO ;
52+ import io .sentrius .sso .install .configuration .dtos .RuleDTO ;
53+ import jakarta .transaction .Transactional ;
4954import lombok .RequiredArgsConstructor ;
5055import lombok .extern .slf4j .Slf4j ;
5156import org .springframework .boot .context .event .ApplicationReadyEvent ;
@@ -70,12 +75,15 @@ public class ConfigurationApplicationTask {
7075
7176 final UserTypeRepository userTypeRepository ;
7277
78+ final RuleService ruleService ;
79+
7380 final UserService userService ;
7481 private final HostGroupService hostGroupService ;
7582
7683 final CryptoService cryptoService ;
7784
7885 @ EventListener (ApplicationReadyEvent .class )
86+ @ Transactional
7987 public void afterStartup () throws IOException , GeneralSecurityException , JSchException , SQLException {
8088 // Your logic here
8189
@@ -131,7 +139,7 @@ public void afterStartup() throws IOException, GeneralSecurityException, JSchExc
131139 log .info ("No configuration file found" );
132140 }
133141 }
134-
142+ @ Transactional
135143 public List <SideEffect > createStaticType (UserType type , boolean action ) throws SQLException ,
136144 GeneralSecurityException {
137145
@@ -160,7 +168,7 @@ public List<SideEffect> createStaticType(UserType type, boolean action) throws S
160168 return sideEffects ;
161169 }
162170
163-
171+ @ Transactional
164172 public List <SideEffect > initialize (InstallConfiguration installConfiguration , boolean action )
165173 throws SQLException ,
166174 GeneralSecurityException ,
@@ -188,19 +196,38 @@ public List<SideEffect> initialize(InstallConfiguration installConfiguration, bo
188196
189197 // create profiles and assign systems
190198
191- var profiles = createHostGroups (sideEffects , installConfiguration , action );
199+ var rules = createRules (sideEffects , installConfiguration , action );
200+
201+ var profiles = createHostGroups (sideEffects , rules , installConfiguration , action );
192202
193203 createUsers (sideEffects , installConfiguration , userTypes , profiles , action );
194-
204+
205+
206+
195207 // create automation assignments
196208
197209 //AppConfig.encryptProperty("initialized", Instant.now().toString());
198210
199211 return sideEffects ;
200212 }
213+ @ Transactional
214+ protected Map <String ,ProfileRule > createRules (
215+ List <SideEffect > sideEffects , InstallConfiguration installConfiguration ,
216+ boolean action
217+ ) {
218+ Map <String ,ProfileRule > rules = new HashMap <>();
219+ var configuredRules = installConfiguration .getRules ();
220+ for (RuleDTO rule : configuredRules ) {
221+ ProfileRule newRule =
222+ ProfileRule .builder ().ruleClass (rule .getRuleClass ()).ruleName (rule .getDisplayName ()).ruleConfig (rule .getConfiguration ()).build ();
223+ newRule = ruleService .saveRule (newRule );
224+ rules .put (rule .getDisplayName (), newRule );
225+ }
226+ return rules ;
227+ }
201228
202-
203- private List <HostGroup > createHostGroups (List <SideEffect > sideEffects , InstallConfiguration installConfiguration ,
229+ @ Transactional
230+ protected List <HostGroup > createHostGroups (List <SideEffect > sideEffects , Map < String , ProfileRule > rules , InstallConfiguration installConfiguration ,
204231 boolean action )
205232 throws JSchException , GeneralSecurityException , IOException {
206233 List <HostGroup > profiles = new ArrayList <>();
@@ -235,12 +262,21 @@ private List<HostGroup> createHostGroups(List<SideEffect> sideEffects, InstallCo
235262 if (!systems .isEmpty ()) {
236263 hostGroup .setHostSystems (systems );
237264 }
265+
266+ for ( var assignedRule : hostGroupDto .getAssignedRules () ) {
267+ if (rules .containsKey (assignedRule )) {
268+ var rule = rules .get (assignedRule );
269+ hostGroup .getRules ().add (rule );
270+ rule .getHostGroups ().add (hostGroup );
271+ }
272+ };
238273 }
239274 var hostGroups = hostGroupService .getHostGroupsByName (hostGroup .getName ());
240275 if (hostGroups .isEmpty ()) {
241276 if (action ) {
242277
243278 hostGroup = hostGroupRepository .save (hostGroup );
279+ log .info ("Creating Host Group {} with {}" , hostGroup .getId (), hostGroupDto .getDisplayName ());
244280 profiles .add (hostGroup );
245281 for (var hs : hostGroup .getHostSystems ()) {
246282 if (null == hs .getHostGroups ()){
@@ -284,7 +320,8 @@ private List<HostGroup> createHostGroups(List<SideEffect> sideEffects, InstallCo
284320 return profiles ;
285321 }
286322
287- private List <SideEffect > createSystems (InstallConfiguration installConfiguration , boolean action ) throws SQLException ,
323+ @ Transactional
324+ protected List <SideEffect > createSystems (InstallConfiguration installConfiguration , boolean action ) throws SQLException ,
288325 GeneralSecurityException {
289326 List <SideEffect > sideEffects = new ArrayList <>();
290327 if (null != installConfiguration .getSystems ()) {
@@ -303,7 +340,8 @@ private List<SideEffect> createSystems(InstallConfiguration installConfiguration
303340 return sideEffects ;
304341 }
305342
306- private boolean shouldInsertSystem (HostSystem systemObj ) {
343+ @ Transactional
344+ protected boolean shouldInsertSystem (HostSystem systemObj ) {
307345 var systems = systemRepository .findByDisplayName (systemObj .getDisplayName ());
308346 if (systems .isEmpty ()) {
309347 return true ;
@@ -316,6 +354,7 @@ private boolean shouldInsertSystem(HostSystem systemObj) {
316354 return true ;
317355 }
318356
357+ @ Transactional
319358 protected List <UserType > createUserTypes (List <SideEffect > sideEffects , InstallConfiguration installConfiguration ,
320359 boolean action ) throws SQLException , GeneralSecurityException {
321360 List <UserType > types = new ArrayList <>();
@@ -365,6 +404,7 @@ protected List<UserType> createUserTypes(List<SideEffect> sideEffects, InstallCo
365404 return types ;
366405 }
367406
407+ @ Transactional
368408 protected List <User > createUsers (
369409 List <SideEffect > sideEffects , InstallConfiguration installConfiguration , List <UserType > userTypes ,
370410 List <HostGroup > profiles , boolean action )
@@ -466,6 +506,8 @@ static void createOrUpdate(Map<Long, Set<Long>> assignments, Long profileId, Lon
466506 set .add (userId );
467507 }
468508
509+
510+ @ Transactional
469511 protected List <SideEffect > createAdminUser (InstallConfiguration installConfiguration , boolean action ) throws NoSuchAlgorithmException {
470512
471513 var user = installConfiguration .getAdminUser ();
@@ -501,6 +543,7 @@ protected List<SideEffect> createAdminUser(InstallConfiguration installConfigura
501543 return sideEffects ;
502544 }
503545
546+ @ Transactional
504547 protected void createSystemUser (InstallConfiguration connection ) throws NoSuchAlgorithmException {
505548
506549 User user = User .builder ()
0 commit comments