You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by di...@apache.org on 2026/08/07 04:47:04 UTC

(airavata) branch refactor-single-module updated: Models and APIs for ssh credential handling

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

DImuthuUpe pushed a commit to branch refactor-single-module
in repository https://gitbox.apache.org/repos/asf/airavata.git


The following commit(s) were added to refs/heads/refactor-single-module by this push:
     new 195dae1c3b Models and APIs for ssh credential handling
195dae1c3b is described below

commit 195dae1c3b494ab6c7af37af35578791620e2daa
Author: DImuthuUpe <di...@gmail.com>
AuthorDate: Fri Aug 7 00:46:58 2026 -0400

    Models and APIs for ssh credential handling
---
 .../org/apache/airavata/api/SSHKeyController.java  | 58 ++++++++++++++
 .../airavata/api/SSHUserCredentialController.java  | 61 ++++++++++++++
 .../airavata/credentials/dto/SSHKeyRequestDto.java | 57 +++++++++++++
 .../credentials/dto/SSHKeyResponseDto.java         | 38 +++++++++
 .../dto/SSHUserCredentialRequestDto.java           | 29 +++++++
 .../dto/SSHUserCredentialResponseDto.java          | 37 +++++++++
 .../airavata/credentials/mapper/SSHKeyMapper.java  | 40 ++++++++++
 .../mapper/SSHUserCredentialMapper.java            | 30 +++++++
 .../airavata/credentials/model/SSHKeyEntity.java   | 70 ++++++++++++++++
 .../credentials/model/SSHUserCredential.java       | 53 ++++++++++++
 .../credentials/repository/SSHKeyRepository.java   | 11 +++
 .../repository/SSHUserCredentialRepository.java    | 14 ++++
 .../credentials/service/SSHKeyService.java         | 93 ++++++++++++++++++++++
 .../service/SSHUserCredentialService.java          | 87 ++++++++++++++++++++
 14 files changed, 678 insertions(+)

diff --git a/src/main/java/org/apache/airavata/api/SSHKeyController.java b/src/main/java/org/apache/airavata/api/SSHKeyController.java
new file mode 100644
index 0000000000..c3b9ce3078
--- /dev/null
+++ b/src/main/java/org/apache/airavata/api/SSHKeyController.java
@@ -0,0 +1,58 @@
+package org.apache.airavata.api;
+
+import jakarta.validation.Valid;
+import java.util.List;
+import org.apache.airavata.credentials.dto.SSHKeyRequestDto;
+import org.apache.airavata.credentials.dto.SSHKeyResponseDto;
+import org.apache.airavata.credentials.service.SSHKeyService;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.RestController;
+
+/** Responses expose only the key id, name and public key — never the private key. */
+@RestController
+@RequestMapping("/api/v1/ssh-keys")
+public class SSHKeyController {
+
+    private final SSHKeyService sshKeyService;
+
+    public SSHKeyController(SSHKeyService sshKeyService) {
+        this.sshKeyService = sshKeyService;
+    }
+
+    @GetMapping
+    public List<SSHKeyResponseDto> getAllKeys() {
+        return sshKeyService.getAllKeys();
+    }
+
+    @GetMapping("/{sshKeyId}")
+    public SSHKeyResponseDto getKey(@PathVariable String sshKeyId) {
+        return sshKeyService.getKey(sshKeyId);
+    }
+
+    @PostMapping
+    @ResponseStatus(HttpStatus.CREATED)
+    public SSHKeyResponseDto createKey(@Valid @RequestBody SSHKeyRequestDto request) {
+        return sshKeyService.createKey(request);
+    }
+
+    /** Omitting {@code privateKey}/{@code passphrase} keeps the stored secrets. */
+    @PutMapping("/{sshKeyId}")
+    public SSHKeyResponseDto updateKey(
+            @PathVariable String sshKeyId, @Valid @RequestBody SSHKeyRequestDto request) {
+        return sshKeyService.updateKey(sshKeyId, request);
+    }
+
+    @DeleteMapping("/{sshKeyId}")
+    @ResponseStatus(HttpStatus.NO_CONTENT)
+    public void deleteKey(@PathVariable String sshKeyId) {
+        sshKeyService.deleteKey(sshKeyId);
+    }
+}
diff --git a/src/main/java/org/apache/airavata/api/SSHUserCredentialController.java b/src/main/java/org/apache/airavata/api/SSHUserCredentialController.java
new file mode 100644
index 0000000000..f6f728d80c
--- /dev/null
+++ b/src/main/java/org/apache/airavata/api/SSHUserCredentialController.java
@@ -0,0 +1,61 @@
+package org.apache.airavata.api;
+
+import jakarta.validation.Valid;
+import java.util.List;
+import org.apache.airavata.credentials.dto.SSHUserCredentialRequestDto;
+import org.apache.airavata.credentials.dto.SSHUserCredentialResponseDto;
+import org.apache.airavata.credentials.service.SSHUserCredentialService;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("/api/v1/ssh-credentials")
+public class SSHUserCredentialController {
+
+    private final SSHUserCredentialService sshUserCredentialService;
+
+    public SSHUserCredentialController(SSHUserCredentialService sshUserCredentialService) {
+        this.sshUserCredentialService = sshUserCredentialService;
+    }
+
+    /** Lists every credential, or only those using {@code sshKeyId} when supplied. */
+    @GetMapping
+    public List<SSHUserCredentialResponseDto> getAllCredentials(@RequestParam(required = false) String sshKeyId) {
+        return sshKeyId == null
+                ? sshUserCredentialService.getAllCredentials()
+                : sshUserCredentialService.getCredentialsByKey(sshKeyId);
+    }
+
+    @GetMapping("/{sshCredentialId}")
+    public SSHUserCredentialResponseDto getCredential(@PathVariable String sshCredentialId) {
+        return sshUserCredentialService.getCredential(sshCredentialId);
+    }
+
+    @PostMapping
+    @ResponseStatus(HttpStatus.CREATED)
+    public SSHUserCredentialResponseDto createCredential(
+            @Valid @RequestBody SSHUserCredentialRequestDto request) {
+        return sshUserCredentialService.createCredential(request);
+    }
+
+    @PutMapping("/{sshCredentialId}")
+    public SSHUserCredentialResponseDto updateCredential(
+            @PathVariable String sshCredentialId, @Valid @RequestBody SSHUserCredentialRequestDto request) {
+        return sshUserCredentialService.updateCredential(sshCredentialId, request);
+    }
+
+    @DeleteMapping("/{sshCredentialId}")
+    @ResponseStatus(HttpStatus.NO_CONTENT)
+    public void deleteCredential(@PathVariable String sshCredentialId) {
+        sshUserCredentialService.deleteCredential(sshCredentialId);
+    }
+}
diff --git a/src/main/java/org/apache/airavata/credentials/dto/SSHKeyRequestDto.java b/src/main/java/org/apache/airavata/credentials/dto/SSHKeyRequestDto.java
new file mode 100644
index 0000000000..f78fe8270b
--- /dev/null
+++ b/src/main/java/org/apache/airavata/credentials/dto/SSHKeyRequestDto.java
@@ -0,0 +1,57 @@
+package org.apache.airavata.credentials.dto;
+
+import jakarta.validation.constraints.NotBlank;
+
+/**
+ * Create/update payload for an SSH key pair.
+ *
+ * <p>{@code privateKey} and {@code passphrase} are write-only: they are accepted here but
+ * never returned by any endpoint. On update, leaving either blank keeps the stored value
+ * rather than clearing it — a client cannot echo back a secret it was never given.
+ */
+public class SSHKeyRequestDto {
+
+    @NotBlank(message = "SSH key name cannot be blank")
+    private String sshKeyName;
+
+    @NotBlank(message = "Public key cannot be blank")
+    private String publicKey;
+
+    /** Required on create; omit on update to keep the stored key. */
+    private String privateKey;
+
+    /** Omit on update to keep the stored passphrase. */
+    private String passphrase;
+
+    public String getSshKeyName() {
+        return sshKeyName;
+    }
+
+    public void setSshKeyName(String sshKeyName) {
+        this.sshKeyName = sshKeyName;
+    }
+
+    public String getPublicKey() {
+        return publicKey;
+    }
+
+    public void setPublicKey(String publicKey) {
+        this.publicKey = publicKey;
+    }
+
+    public String getPrivateKey() {
+        return privateKey;
+    }
+
+    public void setPrivateKey(String privateKey) {
+        this.privateKey = privateKey;
+    }
+
+    public String getPassphrase() {
+        return passphrase;
+    }
+
+    public void setPassphrase(String passphrase) {
+        this.passphrase = passphrase;
+    }
+}
diff --git a/src/main/java/org/apache/airavata/credentials/dto/SSHKeyResponseDto.java b/src/main/java/org/apache/airavata/credentials/dto/SSHKeyResponseDto.java
new file mode 100644
index 0000000000..b648a9c73f
--- /dev/null
+++ b/src/main/java/org/apache/airavata/credentials/dto/SSHKeyResponseDto.java
@@ -0,0 +1,38 @@
+package org.apache.airavata.credentials.dto;
+
+/**
+ * Read view of an SSH key.
+ *
+ * <p>Deliberately carries no {@code privateKey} and no {@code passphrase}. The absence of
+ * those fields is the mechanism that keeps them from leaking — do not add them here.
+ */
+public class SSHKeyResponseDto {
+
+    private String sshKeyId;
+    private String sshKeyName;
+    private String publicKey;
+
+    public String getSshKeyId() {
+        return sshKeyId;
+    }
+
+    public void setSshKeyId(String sshKeyId) {
+        this.sshKeyId = sshKeyId;
+    }
+
+    public String getSshKeyName() {
+        return sshKeyName;
+    }
+
+    public void setSshKeyName(String sshKeyName) {
+        this.sshKeyName = sshKeyName;
+    }
+
+    public String getPublicKey() {
+        return publicKey;
+    }
+
+    public void setPublicKey(String publicKey) {
+        this.publicKey = publicKey;
+    }
+}
diff --git a/src/main/java/org/apache/airavata/credentials/dto/SSHUserCredentialRequestDto.java b/src/main/java/org/apache/airavata/credentials/dto/SSHUserCredentialRequestDto.java
new file mode 100644
index 0000000000..c2821de062
--- /dev/null
+++ b/src/main/java/org/apache/airavata/credentials/dto/SSHUserCredentialRequestDto.java
@@ -0,0 +1,29 @@
+package org.apache.airavata.credentials.dto;
+
+import jakarta.validation.constraints.NotBlank;
+
+/** Create/update payload for an SSH user credential: a username paired with a stored key. */
+public class SSHUserCredentialRequestDto {
+
+    @NotBlank(message = "Username cannot be blank")
+    private String username;
+
+    @NotBlank(message = "SSH key id cannot be blank")
+    private String sshKeyId;
+
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    public String getSshKeyId() {
+        return sshKeyId;
+    }
+
+    public void setSshKeyId(String sshKeyId) {
+        this.sshKeyId = sshKeyId;
+    }
+}
diff --git a/src/main/java/org/apache/airavata/credentials/dto/SSHUserCredentialResponseDto.java b/src/main/java/org/apache/airavata/credentials/dto/SSHUserCredentialResponseDto.java
new file mode 100644
index 0000000000..12c19b172d
--- /dev/null
+++ b/src/main/java/org/apache/airavata/credentials/dto/SSHUserCredentialResponseDto.java
@@ -0,0 +1,37 @@
+package org.apache.airavata.credentials.dto;
+
+/**
+ * Read view of an SSH user credential. The key is inlined as its safe summary — no
+ * private key or passphrase — so callers can show which key backs the credential without
+ * a second request.
+ */
+public class SSHUserCredentialResponseDto {
+
+    private String sshCredentialId;
+    private String username;
+    private SSHKeyResponseDto sshKey;
+
+    public String getSshCredentialId() {
+        return sshCredentialId;
+    }
+
+    public void setSshCredentialId(String sshCredentialId) {
+        this.sshCredentialId = sshCredentialId;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    public SSHKeyResponseDto getSshKey() {
+        return sshKey;
+    }
+
+    public void setSshKey(SSHKeyResponseDto sshKey) {
+        this.sshKey = sshKey;
+    }
+}
diff --git a/src/main/java/org/apache/airavata/credentials/mapper/SSHKeyMapper.java b/src/main/java/org/apache/airavata/credentials/mapper/SSHKeyMapper.java
new file mode 100644
index 0000000000..2e1083597a
--- /dev/null
+++ b/src/main/java/org/apache/airavata/credentials/mapper/SSHKeyMapper.java
@@ -0,0 +1,40 @@
+package org.apache.airavata.credentials.mapper;
+
+import org.apache.airavata.credentials.dto.SSHKeyRequestDto;
+import org.apache.airavata.credentials.dto.SSHKeyResponseDto;
+import org.apache.airavata.credentials.model.SSHKeyEntity;
+import org.mapstruct.Mapper;
+import org.mapstruct.Mapping;
+import org.mapstruct.MappingTarget;
+import org.mapstruct.NullValuePropertyMappingStrategy;
+import org.mapstruct.ReportingPolicy;
+
+/**
+ * Entity/DTO conversions for SSH keys.
+ *
+ * <p>{@code toResponseDto} cannot leak the private key or passphrase because
+ * {@link SSHKeyResponseDto} has nowhere to put them.
+ *
+ * <p>{@code updateEntity} ignores null incoming secrets ({@code IGNORE} rather than the
+ * default {@code SET_TO_NULL}), so a client that round-trips a response — which never
+ * carries those fields — cannot blank out a stored key by omission.
+ */
+@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
+public interface SSHKeyMapper {
+
+    SSHKeyResponseDto toResponseDto(SSHKeyEntity entity);
+
+    @Mapping(target = "sshKeyId", ignore = true)
+    SSHKeyEntity toEntity(SSHKeyRequestDto dto);
+
+    @Mapping(target = "sshKeyId", ignore = true)
+    @Mapping(
+            target = "privateKey",
+            source = "privateKey",
+            nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
+    @Mapping(
+            target = "passphrase",
+            source = "passphrase",
+            nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
+    void updateEntity(SSHKeyRequestDto dto, @MappingTarget SSHKeyEntity entity);
+}
diff --git a/src/main/java/org/apache/airavata/credentials/mapper/SSHUserCredentialMapper.java b/src/main/java/org/apache/airavata/credentials/mapper/SSHUserCredentialMapper.java
new file mode 100644
index 0000000000..ba4f382382
--- /dev/null
+++ b/src/main/java/org/apache/airavata/credentials/mapper/SSHUserCredentialMapper.java
@@ -0,0 +1,30 @@
+package org.apache.airavata.credentials.mapper;
+
+import org.apache.airavata.credentials.dto.SSHUserCredentialRequestDto;
+import org.apache.airavata.credentials.dto.SSHUserCredentialResponseDto;
+import org.apache.airavata.credentials.model.SSHUserCredential;
+import org.mapstruct.Mapper;
+import org.mapstruct.Mapping;
+import org.mapstruct.MappingTarget;
+import org.mapstruct.ReportingPolicy;
+
+/**
+ * Entity/DTO conversions for SSH user credentials. The key association is resolved by the
+ * service from {@code sshKeyId}, so it is never mapped off the request body.
+ */
+@Mapper(
+        componentModel = "spring",
+        uses = SSHKeyMapper.class,
+        unmappedTargetPolicy = ReportingPolicy.IGNORE)
+public interface SSHUserCredentialMapper {
+
+    SSHUserCredentialResponseDto toResponseDto(SSHUserCredential entity);
+
+    @Mapping(target = "sshCredentialId", ignore = true)
+    @Mapping(target = "sshKey", ignore = true)
+    SSHUserCredential toEntity(SSHUserCredentialRequestDto dto);
+
+    @Mapping(target = "sshCredentialId", ignore = true)
+    @Mapping(target = "sshKey", ignore = true)
+    void updateEntity(SSHUserCredentialRequestDto dto, @MappingTarget SSHUserCredential entity);
+}
diff --git a/src/main/java/org/apache/airavata/credentials/model/SSHKeyEntity.java b/src/main/java/org/apache/airavata/credentials/model/SSHKeyEntity.java
new file mode 100644
index 0000000000..61820dc759
--- /dev/null
+++ b/src/main/java/org/apache/airavata/credentials/model/SSHKeyEntity.java
@@ -0,0 +1,70 @@
+package org.apache.airavata.credentials.model;
+
+import jakarta.persistence.Column;
+import jakarta.persistence.Entity;
+import jakarta.persistence.GeneratedValue;
+import jakarta.persistence.GenerationType;
+import jakarta.persistence.Id;
+import jakarta.persistence.Lob;
+
+@Entity
+public class SSHKeyEntity {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.UUID)
+    private String sshKeyId;
+    
+    @Column(nullable = false)
+    private String sshKeyName;
+
+    @Lob
+    @Column(nullable = false)
+    private String publicKey;
+
+    @Lob
+    @Column(nullable = false)
+    private String privateKey;
+
+    @Column(nullable = true)
+    private String passphrase;
+
+    public String getSshKeyId() {
+        return sshKeyId;
+    }
+
+    public void setSshKeyId(String sshKeyId) {
+        this.sshKeyId = sshKeyId;
+    }
+
+    public String getSshKeyName() {
+        return sshKeyName;
+    }
+
+    public void setSshKeyName(String sshKeyName) {
+        this.sshKeyName = sshKeyName;
+    }
+
+    public String getPublicKey() {
+        return publicKey;
+    }
+
+    public void setPublicKey(String publicKey) {
+        this.publicKey = publicKey;
+    }
+
+    public String getPrivateKey() {
+        return privateKey;
+    }
+
+    public void setPrivateKey(String privateKey) {
+        this.privateKey = privateKey;
+    }
+
+    public String getPassphrase() {
+        return passphrase;
+    }
+
+    public void setPassphrase(String passphrase) {
+        this.passphrase = passphrase;
+    }
+}
diff --git a/src/main/java/org/apache/airavata/credentials/model/SSHUserCredential.java b/src/main/java/org/apache/airavata/credentials/model/SSHUserCredential.java
new file mode 100644
index 0000000000..2ecbffe315
--- /dev/null
+++ b/src/main/java/org/apache/airavata/credentials/model/SSHUserCredential.java
@@ -0,0 +1,53 @@
+package org.apache.airavata.credentials.model;
+
+import jakarta.persistence.Column;
+import jakarta.persistence.Entity;
+import jakarta.persistence.ForeignKey;
+import jakarta.persistence.GeneratedValue;
+import jakarta.persistence.GenerationType;
+import jakarta.persistence.Id;
+import jakarta.persistence.JoinColumn;
+import jakarta.persistence.ManyToOne;
+
+@Entity
+public class SSHUserCredential {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.UUID)
+    private String sshCredentialId;
+
+    @Column(nullable = false)
+    private String username;
+
+    // Many-to-one rather than one-to-one: the same registered key can back
+    // credentials
+    // for different usernames, and deleting a credential must not take the key with
+    // it.
+    @ManyToOne
+    @JoinColumn(name = "ssh_key_id", foreignKey = @ForeignKey(name = "fk_credential_ssh_key"))
+    private SSHKeyEntity sshKey;
+
+    public String getSshCredentialId() {
+        return sshCredentialId;
+    }
+
+    public void setSshCredentialId(String sshCredentialId) {
+        this.sshCredentialId = sshCredentialId;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    public SSHKeyEntity getSshKey() {
+        return sshKey;
+    }
+
+    public void setSshKey(SSHKeyEntity sshKey) {
+        this.sshKey = sshKey;
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/org/apache/airavata/credentials/repository/SSHKeyRepository.java b/src/main/java/org/apache/airavata/credentials/repository/SSHKeyRepository.java
new file mode 100644
index 0000000000..ef08f3f236
--- /dev/null
+++ b/src/main/java/org/apache/airavata/credentials/repository/SSHKeyRepository.java
@@ -0,0 +1,11 @@
+package org.apache.airavata.credentials.repository;
+
+import org.apache.airavata.credentials.model.SSHKeyEntity;
+import org.springframework.data.repository.ListCrudRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface SSHKeyRepository extends ListCrudRepository<SSHKeyEntity, String> {
+
+    boolean existsBySshKeyName(String sshKeyName);
+}
diff --git a/src/main/java/org/apache/airavata/credentials/repository/SSHUserCredentialRepository.java b/src/main/java/org/apache/airavata/credentials/repository/SSHUserCredentialRepository.java
new file mode 100644
index 0000000000..7dd14faebd
--- /dev/null
+++ b/src/main/java/org/apache/airavata/credentials/repository/SSHUserCredentialRepository.java
@@ -0,0 +1,14 @@
+package org.apache.airavata.credentials.repository;
+
+import java.util.List;
+import org.apache.airavata.credentials.model.SSHUserCredential;
+import org.springframework.data.repository.ListCrudRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface SSHUserCredentialRepository extends ListCrudRepository<SSHUserCredential, String> {
+
+    List<SSHUserCredential> findBySshKey_SshKeyId(String sshKeyId);
+
+    boolean existsBySshKey_SshKeyId(String sshKeyId);
+}
diff --git a/src/main/java/org/apache/airavata/credentials/service/SSHKeyService.java b/src/main/java/org/apache/airavata/credentials/service/SSHKeyService.java
new file mode 100644
index 0000000000..b809567fee
--- /dev/null
+++ b/src/main/java/org/apache/airavata/credentials/service/SSHKeyService.java
@@ -0,0 +1,93 @@
+package org.apache.airavata.credentials.service;
+
+import java.util.List;
+import org.apache.airavata.credentials.dto.SSHKeyRequestDto;
+import org.apache.airavata.credentials.dto.SSHKeyResponseDto;
+import org.apache.airavata.credentials.mapper.SSHKeyMapper;
+import org.apache.airavata.credentials.model.SSHKeyEntity;
+import org.apache.airavata.credentials.repository.SSHKeyRepository;
+import org.apache.airavata.credentials.repository.SSHUserCredentialRepository;
+import org.springframework.http.HttpStatus;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.server.ResponseStatusException;
+
+/**
+ * CRUD for SSH key pairs.
+ *
+ * <p>The private key and passphrase are write-only across this whole layer: they can be
+ * set and rotated, but no read path returns them.
+ */
+@Service
+public class SSHKeyService {
+
+    private final SSHKeyRepository keyRepository;
+    private final SSHUserCredentialRepository credentialRepository;
+    private final SSHKeyMapper mapper;
+
+    public SSHKeyService(
+            SSHKeyRepository keyRepository,
+            SSHUserCredentialRepository credentialRepository,
+            SSHKeyMapper mapper) {
+        this.keyRepository = keyRepository;
+        this.credentialRepository = credentialRepository;
+        this.mapper = mapper;
+    }
+
+    @Transactional(readOnly = true)
+    public List<SSHKeyResponseDto> getAllKeys() {
+        return keyRepository.findAll().stream().map(mapper::toResponseDto).toList();
+    }
+
+    @Transactional(readOnly = true)
+    public SSHKeyResponseDto getKey(String sshKeyId) {
+        return mapper.toResponseDto(findOrThrow(sshKeyId));
+    }
+
+    @PreAuthorize("hasAnyAuthority('ADMIN', 'SUPER_ADMIN')")
+    @Transactional
+    public SSHKeyResponseDto createKey(SSHKeyRequestDto request) {
+        // Required by the column but not by the request DTO, since update treats it as
+        // optional — so create has to enforce it here rather than through validation.
+        if (request.getPrivateKey() == null || request.getPrivateKey().isBlank()) {
+            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Private key is required when creating a key");
+        }
+        SSHKeyEntity entity = mapper.toEntity(request);
+        return mapper.toResponseDto(keyRepository.save(entity));
+    }
+
+    /** Blank {@code privateKey}/{@code passphrase} leave the stored secrets untouched. */
+    @PreAuthorize("hasAnyAuthority('ADMIN', 'SUPER_ADMIN')")
+    @Transactional
+    public SSHKeyResponseDto updateKey(String sshKeyId, SSHKeyRequestDto request) {
+        SSHKeyEntity entity = findOrThrow(sshKeyId);
+        // MapStruct only skips nulls; normalize blanks to null so an empty string from a
+        // form post is treated as "unchanged" too, not as a wiped secret.
+        if (request.getPrivateKey() != null && request.getPrivateKey().isBlank()) {
+            request.setPrivateKey(null);
+        }
+        if (request.getPassphrase() != null && request.getPassphrase().isBlank()) {
+            request.setPassphrase(null);
+        }
+        mapper.updateEntity(request, entity);
+        return mapper.toResponseDto(keyRepository.save(entity));
+    }
+
+    @PreAuthorize("hasAnyAuthority('ADMIN', 'SUPER_ADMIN')")
+    @Transactional
+    public void deleteKey(String sshKeyId) {
+        SSHKeyEntity entity = findOrThrow(sshKeyId);
+        if (credentialRepository.existsBySshKey_SshKeyId(sshKeyId)) {
+            throw new ResponseStatusException(
+                    HttpStatus.CONFLICT, "Key is in use by a credential and cannot be deleted: " + sshKeyId);
+        }
+        keyRepository.delete(entity);
+    }
+
+    private SSHKeyEntity findOrThrow(String sshKeyId) {
+        return keyRepository
+                .findById(sshKeyId)
+                .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "SSH key not found: " + sshKeyId));
+    }
+}
diff --git a/src/main/java/org/apache/airavata/credentials/service/SSHUserCredentialService.java b/src/main/java/org/apache/airavata/credentials/service/SSHUserCredentialService.java
new file mode 100644
index 0000000000..eb0f3dcfee
--- /dev/null
+++ b/src/main/java/org/apache/airavata/credentials/service/SSHUserCredentialService.java
@@ -0,0 +1,87 @@
+package org.apache.airavata.credentials.service;
+
+import java.util.List;
+import org.apache.airavata.credentials.dto.SSHUserCredentialRequestDto;
+import org.apache.airavata.credentials.dto.SSHUserCredentialResponseDto;
+import org.apache.airavata.credentials.mapper.SSHUserCredentialMapper;
+import org.apache.airavata.credentials.model.SSHKeyEntity;
+import org.apache.airavata.credentials.model.SSHUserCredential;
+import org.apache.airavata.credentials.repository.SSHKeyRepository;
+import org.apache.airavata.credentials.repository.SSHUserCredentialRepository;
+import org.springframework.http.HttpStatus;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.server.ResponseStatusException;
+
+/** CRUD for SSH user credentials, each pairing a username with a registered SSH key. */
+@Service
+public class SSHUserCredentialService {
+
+    private final SSHUserCredentialRepository credentialRepository;
+    private final SSHKeyRepository keyRepository;
+    private final SSHUserCredentialMapper mapper;
+
+    public SSHUserCredentialService(
+            SSHUserCredentialRepository credentialRepository,
+            SSHKeyRepository keyRepository,
+            SSHUserCredentialMapper mapper) {
+        this.credentialRepository = credentialRepository;
+        this.keyRepository = keyRepository;
+        this.mapper = mapper;
+    }
+
+    @Transactional(readOnly = true)
+    public List<SSHUserCredentialResponseDto> getAllCredentials() {
+        return credentialRepository.findAll().stream().map(mapper::toResponseDto).toList();
+    }
+
+    @Transactional(readOnly = true)
+    public List<SSHUserCredentialResponseDto> getCredentialsByKey(String sshKeyId) {
+        return credentialRepository.findBySshKey_SshKeyId(sshKeyId).stream()
+                .map(mapper::toResponseDto)
+                .toList();
+    }
+
+    @Transactional(readOnly = true)
+    public SSHUserCredentialResponseDto getCredential(String sshCredentialId) {
+        return mapper.toResponseDto(findOrThrow(sshCredentialId));
+    }
+
+    @PreAuthorize("hasAnyAuthority('ADMIN', 'SUPER_ADMIN')")
+    @Transactional
+    public SSHUserCredentialResponseDto createCredential(SSHUserCredentialRequestDto request) {
+        SSHUserCredential entity = mapper.toEntity(request);
+        entity.setSshKey(resolveKey(request.getSshKeyId()));
+        return mapper.toResponseDto(credentialRepository.save(entity));
+    }
+
+    @PreAuthorize("hasAnyAuthority('ADMIN', 'SUPER_ADMIN')")
+    @Transactional
+    public SSHUserCredentialResponseDto updateCredential(
+            String sshCredentialId, SSHUserCredentialRequestDto request) {
+        SSHUserCredential entity = findOrThrow(sshCredentialId);
+        mapper.updateEntity(request, entity);
+        entity.setSshKey(resolveKey(request.getSshKeyId()));
+        return mapper.toResponseDto(credentialRepository.save(entity));
+    }
+
+    @PreAuthorize("hasAnyAuthority('ADMIN', 'SUPER_ADMIN')")
+    @Transactional
+    public void deleteCredential(String sshCredentialId) {
+        credentialRepository.delete(findOrThrow(sshCredentialId));
+    }
+
+    private SSHUserCredential findOrThrow(String sshCredentialId) {
+        return credentialRepository
+                .findById(sshCredentialId)
+                .orElseThrow(() -> new ResponseStatusException(
+                        HttpStatus.NOT_FOUND, "SSH credential not found: " + sshCredentialId));
+    }
+
+    private SSHKeyEntity resolveKey(String sshKeyId) {
+        return keyRepository
+                .findById(sshKeyId)
+                .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "SSH key not found: " + sshKeyId));
+    }
+}