11package io .sentrius .sso .controllers .api ;
22
3+ import java .io .IOException ;
4+ import java .io .InputStream ;
35import java .security .GeneralSecurityException ;
46import java .sql .SQLException ;
57import io .sentrius .sso .config .ApiPaths ;
68import io .sentrius .sso .core .config .SystemOptions ;
79import io .sentrius .sso .core .controllers .BaseController ;
810import io .sentrius .sso .core .dto .AgentRegistrationDTO ;
11+ import io .sentrius .sso .core .model .security .IdentityType ;
12+ import io .sentrius .sso .core .model .security .UserType ;
13+ import io .sentrius .sso .core .model .users .User ;
914import io .sentrius .sso .core .services .ATPLPolicyService ;
1015import io .sentrius .sso .core .services .ErrorOutputService ;
1116import io .sentrius .sso .core .services .UserService ;
1924import jakarta .servlet .http .HttpServletRequest ;
2025import jakarta .servlet .http .HttpServletResponse ;
2126import lombok .extern .slf4j .Slf4j ;
27+ import org .springframework .beans .factory .annotation .Value ;
2228import org .springframework .http .ResponseEntity ;
2329import org .springframework .web .bind .annotation .GetMapping ;
2430import org .springframework .web .bind .annotation .PostMapping ;
@@ -39,6 +45,13 @@ public class AgentBootstrapController extends BaseController {
3945 final ZeroTrustRequestService ztrService ;
4046 final AgentService agentService ;
4147
48+
49+ @ Value ("${sentrius.agent.register.bootstrap.allow:false}" )
50+ private boolean allowRegistration ;
51+
52+ @ Value ("${sentrius.agent.bootstrap.policy:default-policy.yaml}" )
53+ private String defaultPolicyFile ;
54+
4255 public AgentBootstrapController (
4356 UserService userService ,
4457 SystemOptions systemOptions ,
@@ -62,22 +75,56 @@ public AgentBootstrapController(
6275
6376 @ PostMapping ("/register" )
6477 // no LimitAccess
65- public ResponseEntity <AgentRegistrationDTO > bootsrap (
66- @ RequestBody AgentRegistrationDTO registrationDTO ) throws GeneralSecurityException {
78+ public ResponseEntity <AgentRegistrationDTO > bootstrap (
79+ @ RequestBody AgentRegistrationDTO registrationDTO ) throws GeneralSecurityException , IOException {
6780 log .info ("Registering agent {}" , registrationDTO );
68- var secret = keycloakService .registerAgentClient (registrationDTO .getAgentName ());
81+ // need a pre-shared secret to register the agent or ztat approval
82+ var unencryptedRegistration = keycloakService .registerAgentClient (registrationDTO );
6983
70- var secretKey = CryptoService .encryptWithPublicKey (secret ,
84+ var secretKey = CryptoService .encryptWithPublicKey (unencryptedRegistration . getClientSecret () ,
7185 CryptoService .decodePublicKey (registrationDTO .getAgentPublicKey (),
7286 registrationDTO .getAgentPublicKeyAlgo ()));
7387
7488 var newDTO = AgentRegistrationDTO .builder ()
75- .agentName (registrationDTO .getAgentName ())
89+ .agentName (unencryptedRegistration .getAgentName ())
7690 .agentPublicKey (registrationDTO .getAgentPublicKey ())
7791 .agentPublicKeyAlgo (registrationDTO .getAgentPublicKeyAlgo ())
7892 .clientSecret (secretKey )
93+ .clientId (unencryptedRegistration .getClientId ())
7994 .build ();
8095
96+ if (allowRegistration ) {
97+ log .info ("Registering {}" , registrationDTO .getAgentName ());
98+ User user = userService .getUserByUsername (newDTO .getAgentName ());
99+ if (user == null ) {
100+ var type = userService .getUserType (
101+ UserType .createUnknownUser ());
102+
103+ user = User .builder ()
104+ .username (newDTO .getAgentName ())
105+ .name (newDTO .getAgentName ())
106+ .emailAddress (newDTO .getAgentName ())
107+ .userId (unencryptedRegistration .getClientId ())
108+ .authorizationType (type .get ())
109+ .identityType (IdentityType .NON_PERSON_ENTITY )
110+ .build ();
111+ log .info ("Creating new user: {}" , user );
112+ userService .save (user );
113+ }
114+ try (InputStream terminalHelperStream = getClass ().getClassLoader ().getResourceAsStream (defaultPolicyFile )) {
115+ if (terminalHelperStream == null ) {
116+ throw new RuntimeException (defaultPolicyFile + "not found on classpath" );
117+
118+ }
119+ String defaultYaml = new String (terminalHelperStream .readAllBytes ());
120+ log .info ("Default policy file: {}" , defaultPolicyFile );
121+ var policy = atplPolicyService .createPolicy (user , defaultYaml );
122+ }
123+
124+ }
125+ else {
126+ log .info ("Not Registering {}" , registrationDTO .getAgentName ());
127+ }
81128 // bootstrap with a default policy
82129 return ResponseEntity .ok (newDTO );
83130 }
0 commit comments