You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ri...@apache.org on 2021/11/10 09:12:23 UTC

[incubator-streampipes] branch dev updated: [STREAMPIPES-426] Properly store client credentials

This is an automated email from the ASF dual-hosted git repository.

riemer pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampipes.git


The following commit(s) were added to refs/heads/dev by this push:
     new 7051a12  [STREAMPIPES-426] Properly store client credentials
7051a12 is described below

commit 7051a12cb63d6a5157ac90b28c3a65a6fb4ffe30
Author: Dominik Riemer <ri...@fzi.de>
AuthorDate: Wed Nov 10 10:12:14 2021 +0100

    [STREAMPIPES-426] Properly store client credentials
---
 .../streampipes/model/client/user/ServiceAccount.java       |  8 ++++++++
 .../java/org/apache/streampipes/rest/impl/UserResource.java | 13 ++++++++++---
 .../streampipes/user/management/jwt/SpKeyResolver.java      |  4 +++-
 ui/src/app/_enums/user-role.enum.ts                         |  1 +
 .../edit-user-dialog/edit-user-dialog.component.html        |  1 +
 .../edit-user-dialog/edit-user-dialog.component.ts          | 10 +++++++---
 ui/src/app/core-model/gen/streampipes-model-client.ts       |  4 +++-
 ui/src/app/services/available-roles.service.ts              |  1 +
 8 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/streampipes-model-client/src/main/java/org/apache/streampipes/model/client/user/ServiceAccount.java b/streampipes-model-client/src/main/java/org/apache/streampipes/model/client/user/ServiceAccount.java
index 57b22d4..ea45b7d 100644
--- a/streampipes-model-client/src/main/java/org/apache/streampipes/model/client/user/ServiceAccount.java
+++ b/streampipes-model-client/src/main/java/org/apache/streampipes/model/client/user/ServiceAccount.java
@@ -25,6 +25,7 @@ import java.util.Set;
 public class ServiceAccount extends Principal {
 
   private String clientSecret;
+  private boolean secretEncrypted;
 
   public static ServiceAccount from(String serviceAccountName,
                                  String clientSecret,
@@ -51,4 +52,11 @@ public class ServiceAccount extends Principal {
     this.clientSecret = clientSecret;
   }
 
+  public boolean isSecretEncrypted() {
+    return secretEncrypted;
+  }
+
+  public void setSecretEncrypted(boolean secretEncrypted) {
+    this.secretEncrypted = secretEncrypted;
+  }
 }
diff --git a/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/UserResource.java b/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/UserResource.java
index c70a7b3..0cb998c 100644
--- a/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/UserResource.java
+++ b/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/UserResource.java
@@ -21,6 +21,7 @@ import org.apache.streampipes.model.client.user.*;
 import org.apache.streampipes.model.message.Notifications;
 import org.apache.streampipes.rest.core.base.impl.AbstractAuthGuardedRestResource;
 import org.apache.streampipes.rest.shared.annotation.JacksonSerialized;
+import org.apache.streampipes.user.management.encryption.SecretEncryptionManager;
 import org.apache.streampipes.user.management.service.TokenService;
 import org.apache.streampipes.user.management.util.PasswordUtil;
 import org.springframework.security.core.context.SecurityContextHolder;
@@ -142,10 +143,12 @@ public class UserResource extends AbstractAuthGuardedRestResource {
   @JacksonSerialized
   @Produces(MediaType.APPLICATION_JSON)
   @Consumes(MediaType.APPLICATION_JSON)
-  public Response registerService(ServiceAccount userAccount) {
+  public Response registerService(ServiceAccount serviceAccount) {
     // TODO check if userId is already taken
-    if (getUserStorage().getUser(userAccount.getUsername()) == null) {
-      getUserStorage().storeUser(userAccount);
+    if (getUserStorage().getUser(serviceAccount.getUsername()) == null) {
+      serviceAccount.setClientSecret(SecretEncryptionManager.encrypt(serviceAccount.getClientSecret()));
+      serviceAccount.setSecretEncrypted(true);
+      getUserStorage().storeUser(serviceAccount);
       return ok();
     } else {
       return badRequest(Notifications.error("This user ID already exists. Please choose another address."));
@@ -191,6 +194,10 @@ public class UserResource extends AbstractAuthGuardedRestResource {
     if (user != null && (authenticatedUserId.equals(principalId) || isAdmin())) {
       Principal existingUser = getPrincipalById(principalId);
       user.setRev(existingUser.getRev());
+      if (!user.isSecretEncrypted()) {
+        user.setClientSecret(SecretEncryptionManager.encrypt(user.getClientSecret()));
+        user.setSecretEncrypted(true);
+      }
       getUserStorage().updateUser(user);
       return ok(Notifications.success("User updated"));
     } else {
diff --git a/streampipes-user-management/src/main/java/org/apache/streampipes/user/management/jwt/SpKeyResolver.java b/streampipes-user-management/src/main/java/org/apache/streampipes/user/management/jwt/SpKeyResolver.java
index bcf3635..24bbaea 100644
--- a/streampipes-user-management/src/main/java/org/apache/streampipes/user/management/jwt/SpKeyResolver.java
+++ b/streampipes-user-management/src/main/java/org/apache/streampipes/user/management/jwt/SpKeyResolver.java
@@ -26,6 +26,7 @@ import org.apache.streampipes.model.client.user.ServiceAccount;
 import org.apache.streampipes.model.client.user.UserAccount;
 import org.apache.streampipes.storage.api.IUserStorage;
 import org.apache.streampipes.storage.management.StorageDispatcher;
+import org.apache.streampipes.user.management.encryption.SecretEncryptionManager;
 
 import java.nio.charset.StandardCharsets;
 import java.security.Key;
@@ -49,7 +50,8 @@ public class SpKeyResolver implements SigningKeyResolver {
     } else if (isRealUser(principal)) {
       return makeKeyForSecret(this.tokenSecret);
     } else {
-      return makeKeyForSecret(((ServiceAccount) principal).getClientSecret());
+      String decryptedSecret = SecretEncryptionManager.decrypt(((ServiceAccount) principal).getClientSecret());
+      return makeKeyForSecret(decryptedSecret);
     }
   }
 
diff --git a/ui/src/app/_enums/user-role.enum.ts b/ui/src/app/_enums/user-role.enum.ts
index 68175f7..488ee4e 100644
--- a/ui/src/app/_enums/user-role.enum.ts
+++ b/ui/src/app/_enums/user-role.enum.ts
@@ -18,6 +18,7 @@
 
 export enum UserRole {
   ROLE_ADMIN = 'ROLE_ADMIN',
+  ROLE_SERVICE_ADMIN = 'ROLE_SERVICE_ADMIN',
   ROLE_PIPELINE_ADMIN = 'ROLE_PIPELINE_ADMIN',
   ROLE_DASHBOARD_ADMIN = 'ROLE_DASHBOARD_ADMIN',
   ROLE_DATA_EXPLORER_ADMIN = 'ROLE_DATA_EXPLORER_ADMIN',
diff --git a/ui/src/app/configuration/security-configuration/edit-user-dialog/edit-user-dialog.component.html b/ui/src/app/configuration/security-configuration/edit-user-dialog/edit-user-dialog.component.html
index 3dcc8a6..f1333a4 100644
--- a/ui/src/app/configuration/security-configuration/edit-user-dialog/edit-user-dialog.component.html
+++ b/ui/src/app/configuration/security-configuration/edit-user-dialog/edit-user-dialog.component.html
@@ -74,6 +74,7 @@
                                matInput
                                required>
                     </mat-form-field>
+                    <mat-error *ngIf="parentForm.controls.clientSecret.errors">Minimum length 35 characters.</mat-error>
                 </div>
                 <div fxLayout="column" class="general-options-panel">
                     <span class="general-options-header">Groups</span>
diff --git a/ui/src/app/configuration/security-configuration/edit-user-dialog/edit-user-dialog.component.ts b/ui/src/app/configuration/security-configuration/edit-user-dialog/edit-user-dialog.component.ts
index d075178..89c0e8d 100644
--- a/ui/src/app/configuration/security-configuration/edit-user-dialog/edit-user-dialog.component.ts
+++ b/ui/src/app/configuration/security-configuration/edit-user-dialog/edit-user-dialog.component.ts
@@ -71,7 +71,8 @@ export class EditUserDialogComponent implements OnInit {
   }
 
   ngOnInit(): void {
-    this.availableRoles = this.availableRolesService.availableRoles;
+    const filterObject = this.user instanceof UserAccount ? UserRole.ROLE_SERVICE_ADMIN : UserRole.ROLE_ADMIN;
+    this.availableRoles = this.availableRolesService.availableRoles.filter(role => role.role !== filterObject);
     this.userGroupService.getAllUserGroups().subscribe(response => {
       this.availableGroups = response;
     });
@@ -87,7 +88,7 @@ export class EditUserDialogComponent implements OnInit {
     if (this.clonedUser instanceof UserAccount) {
       this.parentForm.addControl('fullName', new FormControl(this.clonedUser.fullName));
     } else {
-      this.parentForm.addControl('clientSecret', new FormControl(this.clonedUser.clientSecret));
+      this.parentForm.addControl('clientSecret', new FormControl(this.clonedUser.clientSecret, [Validators.required, Validators.minLength(35)]));
     }
 
     if (!this.editMode && this.clonedUser instanceof UserAccount) {
@@ -106,7 +107,10 @@ export class EditUserDialogComponent implements OnInit {
           this.clonedUser.password = v.password;
         }
       } else {
-        this.clonedUser.clientSecret = v.clientSecret;
+        if (this.user.clientSecret !== v.clientSecret) {
+          this.clonedUser.clientSecret = v.clientSecret;
+          this.clonedUser.secretEncrypted = false;
+        }
       }
     });
   }
diff --git a/ui/src/app/core-model/gen/streampipes-model-client.ts b/ui/src/app/core-model/gen/streampipes-model-client.ts
index 957e0f2..ac2883a 100644
--- a/ui/src/app/core-model/gen/streampipes-model-client.ts
+++ b/ui/src/app/core-model/gen/streampipes-model-client.ts
@@ -18,7 +18,7 @@
 /* tslint:disable */
 /* eslint-disable */
 // @ts-nocheck
-// Generated using typescript-generator version 2.27.744 on 2021-11-08 19:00:56.
+// Generated using typescript-generator version 2.27.744 on 2021-11-10 09:40:43.
 
 export class ExtensionsServiceEndpointItem {
     appId: string;
@@ -189,6 +189,7 @@ export class RawUserApiToken {
 
 export class ServiceAccount extends Principal {
     clientSecret: string;
+    secretEncrypted: boolean;
 
     static fromData(data: ServiceAccount, target?: ServiceAccount): ServiceAccount {
         if (!data) {
@@ -197,6 +198,7 @@ export class ServiceAccount extends Principal {
         const instance = target || new ServiceAccount();
         super.fromData(data, instance);
         instance.clientSecret = data.clientSecret;
+        instance.secretEncrypted = data.secretEncrypted;
         return instance;
     }
 }
diff --git a/ui/src/app/services/available-roles.service.ts b/ui/src/app/services/available-roles.service.ts
index e6b3024..a9aa7f9 100644
--- a/ui/src/app/services/available-roles.service.ts
+++ b/ui/src/app/services/available-roles.service.ts
@@ -25,6 +25,7 @@ export class AvailableRolesService {
 
   availableRoles: RoleDescription[] = [
     {role: UserRole.ROLE_ADMIN, roleTitle: 'Admin', roleDescription: ''},
+    {role: UserRole.ROLE_SERVICE_ADMIN, roleTitle: 'Service Admin', roleDescription: ''},
     {role: UserRole.ROLE_APP_USER, roleTitle: 'App User', roleDescription: ''},
     {role: UserRole.ROLE_DASHBOARD_USER, roleTitle: 'Dashboard User', roleDescription: ''},
     {role: UserRole.ROLE_DASHBOARD_ADMIN, roleTitle: 'Dashboard Admin', roleDescription: ''},