1111import io .binarycodes .homelab .lib .SignedPublicKeyDownload ;
1212import io .binarycodes .homelab .sshkeysigner .config .ApplicationProperties ;
1313import lombok .extern .log4j .Log4j2 ;
14- import org .apache .commons .io .FilenameUtils ;
1514import org .springframework .stereotype .Service ;
1615
1716import java .io .IOException ;
2625public class KeyService {
2726 /* https://jadaptive.com/app/manpage/en/article/2895616 */
2827
29- private static final String CERTIFICATE_FILE_NAME_SUFFIX = "cert" ;
3028 private final ApplicationProperties applicationProperties ;
3129
3230 public KeyService (final ApplicationProperties applicationProperties ) {
@@ -113,48 +111,48 @@ private SshKeyPair keyInfoToKeyPair(final KeyInfo keyInfo, final String passphra
113111 /**
114112 * Signs the given key for user
115113 */
116- public Optional <SignedPublicKeyDownload > signUserKey (final String filename , final String pubKey , final String keyId , final String principal ) {
117- return signUserKey (filename , pubKey , keyId , List .of (principal ));
114+ public Optional <SignedPublicKeyDownload > signUserKey (final String pubKey , final String keyId , final String principal ) {
115+ return signUserKey (pubKey , keyId , List .of (principal ));
118116 }
119117
120118 /**
121119 * Signs the given key for user
122120 */
123- public Optional <SignedPublicKeyDownload > signUserKey (final String filename , final String pubKey , final String keyId , final List <String > principals ) {
121+ public Optional <SignedPublicKeyDownload > signUserKey (final String pubKey , final String keyId , final List <String > principals ) {
124122 final var bytes = pubKey .getBytes (StandardCharsets .UTF_8 );
125- return signUserKey (filename , bytes , keyId , principals );
123+ return signUserKey (bytes , keyId , principals );
126124 }
127125
128126 /**
129127 * Signs the given key for user
130128 */
131- public Optional <SignedPublicKeyDownload > signUserKey (final String filename , final byte [] bytes , final String keyId , final List <String > principals ) {
132- return signKey (SshCertificateType .USER , filename , bytes , keyId , principals , applicationProperties .caUserValidity ());
129+ public Optional <SignedPublicKeyDownload > signUserKey (final byte [] bytes , final String keyId , final List <String > principals ) {
130+ return signKey (SshCertificateType .USER , bytes , keyId , principals , applicationProperties .caUserValidity ());
133131 }
134132
135133 /**
136134 * Signs the given key for host
137135 */
138- public Optional <SignedPublicKeyDownload > signHostKey (final String filename , final String pubKey , final String keyId , final String principal ) {
139- return signHostKey (filename , pubKey , keyId , List .of (principal ));
136+ public Optional <SignedPublicKeyDownload > signHostKey (final String pubKey , final String keyId , final String principal ) {
137+ return signHostKey (pubKey , keyId , List .of (principal ));
140138 }
141139
142140 /**
143141 * Signs the given key for host
144142 */
145- public Optional <SignedPublicKeyDownload > signHostKey (final String filename , final String pubKey , final String keyId , final List <String > principals ) {
143+ public Optional <SignedPublicKeyDownload > signHostKey (final String pubKey , final String keyId , final List <String > principals ) {
146144 final var bytes = pubKey .getBytes (StandardCharsets .UTF_8 );
147- return signHostKey (filename , bytes , keyId , principals );
145+ return signHostKey (bytes , keyId , principals );
148146 }
149147
150148 /**
151149 * Signs the given key for host
152150 */
153- public Optional <SignedPublicKeyDownload > signHostKey (final String filename , final byte [] bytes , final String keyId , final List <String > principals ) {
154- return signKey (SshCertificateType .HOST , filename , bytes , keyId , principals , applicationProperties .caHostValidity ());
151+ public Optional <SignedPublicKeyDownload > signHostKey (final byte [] bytes , final String keyId , final List <String > principals ) {
152+ return signKey (SshCertificateType .HOST , bytes , keyId , principals , applicationProperties .caHostValidity ());
155153 }
156154
157- private Optional <SignedPublicKeyDownload > signKey (final SshCertificateType certType , final String filename , final byte [] bytes , final String keyId , final List <String > principals , final Duration validitySeconds ) {
155+ private Optional <SignedPublicKeyDownload > signKey (final SshCertificateType certType , final byte [] bytes , final String keyId , final List <String > principals , final Duration validitySeconds ) {
158156 try {
159157 final var publicKeyFileToSign = SshPublicKeyFileFactory .parse (bytes );
160158 final var keyPairToSign = SshKeyPair .getKeyPair (null , publicKeyFileToSign .toPublicKey ());
@@ -168,9 +166,8 @@ private Optional<SignedPublicKeyDownload> signKey(final SshCertificateType certT
168166
169167 final var signedKey = SshPublicKeyFileFactory .create (signed .getCertificate (), publicKeyFileToSign .getComment (), SshPublicKeyFileFactory .OPENSSH_FORMAT );
170168 final var signedKeyString = new String (signedKey .getFormattedKey (), StandardCharsets .UTF_8 );
171- final var downloadFilename = "%s-%s.%s" .formatted (FilenameUtils .getBaseName (filename ), CERTIFICATE_FILE_NAME_SUFFIX , FilenameUtils .getExtension (filename ));
172169
173- return Optional .of (new SignedPublicKeyDownload (downloadFilename , signedKeyString ));
170+ return Optional .of (new SignedPublicKeyDownload (signedKeyString ));
174171 } catch (final IOException e ) {
175172 log .error (e .getMessage (), e );
176173 } catch (final InvalidPassphraseException | SshException e ) {
0 commit comments