Skip to content

Commit cda9d08

Browse files
committed
refactor(identity): add createIdentity overload and align temporary setup
1 parent a542a38 commit cda9d08

8 files changed

Lines changed: 211 additions & 106 deletions

File tree

kura/org.eclipse.kura.api/src/main/java/org/eclipse/kura/identity/IdentityService.java

Lines changed: 103 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -34,76 +34,101 @@ public interface IdentityService {
3434
/**
3535
* Creates a new identity with the given name.
3636
*
37-
* @param identityName the name of the identity to be created.
37+
* @param identityName
38+
* the name of the identity to be created.
3839
* @return {@code true} if the identity with the given name has been created as
3940
* part of the method call or {@code false} if the identity already
40-
* exist.
41-
* @throws KuraException if a failure occurs in creating the identity.
41+
* exists.
42+
* @throws KuraException
43+
* if a failure occurs in creating the identity.
4244
*/
4345
public boolean createIdentity(final String identityName) throws KuraException;
4446

47+
/**
48+
* Creates a new identity using the provided configuration.
49+
*
50+
* @param identityConfiguration
51+
* the identity configuration including identity
52+
* name and optional configuration components.
53+
* @return {@code true} if the identity with the given name has been created as
54+
* part of the method call or {@code false} if the identity already
55+
* exists.
56+
* @throws KuraException
57+
* if a failure occurs in creating the identity.
58+
* @since 2.8.0
59+
*/
60+
public boolean createIdentity(final IdentityConfiguration identityConfiguration) throws KuraException;
61+
4562
/**
4663
* Deletes the identity with the given name, including temporary identities.
4764
*
48-
* @param identityName the name of the identity to be deleted.
65+
* @param identityName
66+
* the name of the identity to be deleted.
4967
* @return {@code true} if the identity with the given name has been deleted as
5068
* part of the method call or {@code false} if the identity does not
5169
* exist.
52-
* @throws KuraException if a failure occurs in deleting the identity.
70+
* @throws KuraException
71+
* if a failure occurs in deleting the identity.
5372
*/
5473
public boolean deleteIdentity(final String identityName) throws KuraException;
5574

5675
/**
5776
* Returns the configuration of all existing identities.
5877
*
59-
* @param componentsToReturn the set of {@link IdentityConfigurationComponent}
60-
* types to be returned. If the set is empty a
61-
* {@link IdentityConfiguration} will be returned for
62-
* each defined identity with an empty component list.
63-
* This can be used to get the name for all defined
64-
* identities.
78+
* @param componentsToReturn
79+
* the set of {@link IdentityConfigurationComponent}
80+
* types to be returned. If the set is empty a
81+
* {@link IdentityConfiguration} will be returned for
82+
* each defined identity with an empty component list.
83+
* This can be used to get the name for all defined
84+
* identities.
6585
*
6686
* @return the list of {@link IdentityConfiguration}s. An empty list will be
6787
* returned if no identities are defined.
68-
* @throws KuraException if a failure occurs in retrieving identity
69-
* configurations.
88+
* @throws KuraException
89+
* if a failure occurs in retrieving identity
90+
* configurations.
7091
*/
7192
public List<IdentityConfiguration> getIdentitiesConfiguration(
72-
Set<Class<? extends IdentityConfigurationComponent>> componentsToReturn)
73-
throws KuraException;
93+
Set<Class<? extends IdentityConfigurationComponent>> componentsToReturn) throws KuraException;
7494

7595
/**
7696
* Returns the configuration of the identity with the given name.
7797
*
78-
* @param identityName the identity name.
79-
* @param componentsToReturn the set of {@link IdentityConfigurationComponent}
80-
* types to be returned.
98+
* @param identityName
99+
* the identity name.
100+
* @param componentsToReturn
101+
* the set of {@link IdentityConfigurationComponent}
102+
* types to be returned.
81103
* @return the configuration of the requested identity or an empty optional if
82104
* the identity does not exist.
83-
* @throws KuraException if a failure occurs in retrieving identity
84-
* configuration.
105+
* @throws KuraException
106+
* if a failure occurs in retrieving identity
107+
* configuration.
85108
*/
86109
public Optional<IdentityConfiguration> getIdentityConfiguration(final String identityName,
87-
Set<Class<? extends IdentityConfigurationComponent>> componentsToReturn)
88-
throws KuraException;
110+
Set<Class<? extends IdentityConfigurationComponent>> componentsToReturn) throws KuraException;
89111

90112
/**
91113
* Returns the default configuration for the identity with the given name, this
92114
* method should succeed even if the identity does not exist. The result should
93115
* be the same configuration returned by the
94-
* {@link IdentityService#getIdentityConfiguration(String, List)}
116+
* {@link IdentityService#getIdentityConfiguration(String, Set)}
95117
* method for an identity that has just been created with the
96118
* {@link IdentityService#createIdentity(String)} method.
97119
*
98120
* This method can be useful for example to allow a user interface to show the
99121
* initial identity configuration to the user before creating it.
100122
*
101-
* @param identityName the identity name.
102-
* @param componentsToReturn the set of {@link IdentityConfigurationComponent}
103-
* types to be returned.
123+
* @param identityName
124+
* the identity name.
125+
* @param componentsToReturn
126+
* the set of {@link IdentityConfigurationComponent}
127+
* types to be returned.
104128
* @return the default configuration for the requested identity
105-
* @throws KuraException if a failure occurs in retrieving identity
106-
* configuration.
129+
* @throws KuraException
130+
* if a failure occurs in retrieving identity
131+
* configuration.
107132
*/
108133
public IdentityConfiguration getIdentityDefaultConfiguration(final String identityName,
109134
Set<Class<? extends IdentityConfigurationComponent>> componentsToReturn) throws KuraException;
@@ -112,48 +137,54 @@ public IdentityConfiguration getIdentityDefaultConfiguration(final String identi
112137
* Validates the provided identity configuration without performing any
113138
* change to the system.
114139
*
115-
* @param identityConfiguration the identity configuration that should be
116-
* validated.
117-
* @throws KuraException if the provided identity configuration is not
118-
* valid.
140+
* @param identityConfiguration
141+
* the identity configuration that should be
142+
* validated.
143+
* @throws KuraException
144+
* if the provided identity configuration is not
145+
* valid.
119146
*/
120-
public void validateIdentityConfiguration(final IdentityConfiguration identityConfiguration)
121-
throws KuraException;
147+
public void validateIdentityConfiguration(final IdentityConfiguration identityConfiguration) throws KuraException;
122148

123149
/**
124150
* Updates the configuration of the given identity for the provided
125151
* {@link IdentityConfigurationComponent} types.
126152
* The configuration of the identities or identity
127153
* components that have not been provided will not be modified.
128154
*
129-
* @param identityConfiguration the identity configuration that should be
130-
* updated.
131-
* @throws KuraException if a failure occurs updating identity
132-
* configuration.
155+
* @param identityConfiguration
156+
* the identity configuration that should be
157+
* updated.
158+
* @throws KuraException
159+
* if a failure occurs updating identity
160+
* configuration.
133161
*/
134-
public void updateIdentityConfiguration(final IdentityConfiguration identityConfiguration)
135-
throws KuraException;
162+
public void updateIdentityConfiguration(final IdentityConfiguration identityConfiguration) throws KuraException;
136163

137164
/**
138165
* Defines a new permission.
139166
*
140-
* @param permission the permission to be created.
167+
* @param permission
168+
* the permission to be created.
141169
* @return {@code true} if the permission has been created as
142170
* part of the method call or {@code false} if the permission already
143171
* exist.
144-
* @throws KuraException if a failure occurs creating the permission.
172+
* @throws KuraException
173+
* if a failure occurs creating the permission.
145174
*/
146175
public boolean createPermission(final Permission permission) throws KuraException;
147176

148177
/**
149178
* Removes an existing permission. The permission will also be removed from all
150179
* identities assigned to it.
151180
*
152-
* @param permission the permission to be deleted.
181+
* @param permission
182+
* the permission to be deleted.
153183
* @return {@code true} if the permission has been deleted as
154184
* part of the method call or {@code false} if the permission does not
155185
* exist.
156-
* @throws KuraException if a failure occurs deleting the permission.
186+
* @throws KuraException
187+
* if a failure occurs deleting the permission.
157188
*/
158189
public boolean deletePermission(final Permission permission) throws KuraException;
159190

@@ -163,17 +194,20 @@ public void updateIdentityConfiguration(final IdentityConfiguration identityConf
163194
*
164195
* @return the set of permissions that are currently defined within the
165196
* framework.
166-
* @throws KuraException if a failure occurs retrieving the permission set.
197+
* @throws KuraException
198+
* if a failure occurs retrieving the permission set.
167199
*/
168200
public Set<Permission> getPermissions() throws KuraException;
169201

170202
/**
171203
* Computes a {@link PasswordHash} for the given plaintext password. The
172204
* password array will be overwritten at the end of the operation.
173205
*
174-
* @param password the plaintext password.
206+
* @param password
207+
* the plaintext password.
175208
* @return the computed password hash.
176-
* @throws KuraException if a failure occurs computing the password hash
209+
* @throws KuraException
210+
* if a failure occurs computing the password hash
177211
*/
178212
public PasswordHash computePasswordHash(final char[] password) throws KuraException;
179213

@@ -183,9 +217,10 @@ public void updateIdentityConfiguration(final IdentityConfiguration identityConf
183217
*
184218
* @param identityName
185219
* @param password
186-
* @throws KuraException if the passwords do not match of if a failure occurs
187-
* while
188-
* performing the check.
220+
* @throws KuraException
221+
* if the passwords do not match of if a failure occurs
222+
* while
223+
* performing the check.
189224
*/
190225
public void checkPassword(final String identityName, final char[] password) throws KuraException;
191226

@@ -195,30 +230,31 @@ public void updateIdentityConfiguration(final IdentityConfiguration identityConf
195230
*
196231
* @param identityName
197232
* @param permission
198-
* @throws KuraException if the provided permissio is not currently assigned to
199-
* the given identity or if occurs while performing the
200-
* check.
233+
* @throws KuraException
234+
* if the provided permissio is not currently assigned to
235+
* the given identity or if occurs while performing the
236+
* check.
201237
*
202238
*/
203239
public void checkPermission(final String identityName, final Permission permission) throws KuraException;
204240

205241
/**
206-
* Creates a temporary identity that is not persisted and has automatic expiration.
207-
* Temporary identities behave like regular identities but are stored in-memory only
208-
* and will be automatically removed after the specified lifetime period.
209-
* The identity name is extracted from {@link IdentityConfiguration#getName()}.
210-
* If a {@link PasswordConfiguration} with a new password is provided, the password
211-
* will be validated and hashed before storage.
242+
* Creates a temporary identity that is not persisted and has automatic
243+
* expiration. Temporary identities behave like regular identities but are
244+
* stored in-memory only and are automatically removed after the specified
245+
* lifetime period.
212246
*
213-
* @param configuration the identity configuration including the identity name, passwords,
214-
* certificates, tokens, permissions, etc.
215-
* @param lifetime the duration before automatic expiration. The identity will be automatically
216-
* removed after this period.
217-
* @throws KuraException if a failure occurs in creating the temporary identity or if an identity
218-
* with the given name already exists (either regular or temporary).
247+
* @param identityName
248+
* the name of the temporary identity to create.
249+
* @param lifetime
250+
* the duration before automatic expiration. The identity
251+
* will be automatically removed after this period.
252+
* @throws KuraException
253+
* if a failure occurs in creating the temporary identity
254+
* or if an identity with the given name already exists
255+
* (either regular or temporary).
219256
* @since 2.8.0
220257
*/
221-
public void createTemporaryIdentity(final IdentityConfiguration configuration,
222-
final Duration lifetime) throws KuraException;
258+
public void createTemporaryIdentity(final String identityName, final Duration lifetime) throws KuraException;
223259

224260
}

kura/org.eclipse.kura.container.provider/src/main/java/org/eclipse/kura/container/provider/ContainerInstance.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,8 @@ private String createTemporaryIdentityWithValidName(final ContainerInstanceOptio
452452
final IdentityConfiguration configuration = new IdentityConfiguration(candidateName,
453453
Arrays.asList(passwordConfiguration, assignedPermissions));
454454

455-
ContainerInstance.this.identityService.createTemporaryIdentity(configuration, Duration.ofDays(365));
455+
ContainerInstance.this.identityService.createTemporaryIdentity(candidateName, Duration.ofDays(365));
456+
ContainerInstance.this.identityService.updateIdentityConfiguration(configuration);
456457
return candidateName;
457458

458459
} catch (final KuraException e) {

kura/org.eclipse.kura.core.identity/src/main/java/org/eclipse/kura/core/identity/IdentityServiceImpl.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,26 @@ public void deactivate() {
120120

121121
@Override
122122
public synchronized boolean createIdentity(final String name) throws KuraException {
123-
if (this.userAdminHelper.getUser(name).isPresent()) {
123+
return createIdentity(new IdentityConfiguration(name, Collections.emptyList()));
124+
}
125+
126+
@Override
127+
public synchronized boolean createIdentity(final IdentityConfiguration configuration) throws KuraException {
128+
final String name = configuration.getName();
129+
130+
if (this.temporaryIdentityStore.exists(name) || this.userAdminHelper.getUser(name).isPresent()) {
124131
return false;
125132
}
126133

127134
audit(() -> {
128135
ValidationUtil.validateNewIdentityName(name);
129136

130137
this.userAdminHelper.createUser(name);
138+
139+
if (!configuration.getComponents().isEmpty()) {
140+
validateIdentityConfiguration(configuration);
141+
this.userAdminIdentityStore.updateIdentityConfiguration(configuration);
142+
}
131143
}, "Create identity " + name);
132144

133145
return true;
@@ -394,13 +406,9 @@ private void validateAdditionalConfigurations(final IdentityConfiguration identi
394406
}
395407
}
396408

397-
// New unified temporary identity methods (IdentityService interface)
398-
399409
@Override
400-
public synchronized void createTemporaryIdentity(IdentityConfiguration configuration,
401-
Duration lifetime) throws KuraException {
402-
403-
final String identityName = configuration.getName();
410+
public synchronized void createTemporaryIdentity(final String identityName, final Duration lifetime)
411+
throws KuraException {
404412

405413
// Check if identity already exists (temporary or regular)
406414
if (this.temporaryStore.exists(identityName)) {
@@ -420,10 +428,8 @@ public synchronized void createTemporaryIdentity(IdentityConfiguration configura
420428
"Temporary identity lifetime must be positive");
421429
}
422430

423-
validateIdentityConfiguration(configuration);
424-
425-
// Process the configuration to hash passwords before storage
426-
this.temporaryIdentityStore.createIdentity(configuration, lifetime);
431+
this.temporaryIdentityStore.createIdentity(new IdentityConfiguration(identityName, Collections.emptyList()),
432+
lifetime);
427433

428434
}, "Create temporary identity " + identityName);
429435
}

kura/org.eclipse.kura.rest.identity.provider/src/main/java/org/eclipse/kura/internal/rest/identity/provider/IdentityRestServiceV2.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2024, 2025 Eurotech and/or its affiliates and others
2+
* Copyright (c) 2024, 2026 Eurotech and/or its affiliates and others
33
*
44
* This program and the accompanying materials are made
55
* available under the terms of the Eclipse Public License 2.0
@@ -10,6 +10,8 @@
1010
* Contributors:
1111
* Eurotech
1212
*******************************************************************************/
13+
// Content with portions generated by generative AI platform
14+
1315
package org.eclipse.kura.internal.rest.identity.provider;
1416

1517
import java.util.Arrays;
@@ -127,7 +129,8 @@ public Response createIdentity(final IdentityDTO identity) {
127129

128130
StringUtils.validateField(NAME_REQUEST_FIELD, identity.getName());
129131

130-
boolean created = this.identityService.createIdentity(identity.getName());
132+
boolean created = this.identityService
133+
.createIdentity(new IdentityConfiguration(identity.getName(), List.of()));
131134
if (!created) {
132135
throw DefaultExceptionHandler.buildWebApplicationException(Status.CONFLICT, "Identity already exists");
133136
}

0 commit comments

Comments
 (0)