|
| 1 | +/* |
| 2 | + * Copyright 2018-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.config.server.config; |
| 18 | + |
| 19 | +import java.security.KeyFactory; |
| 20 | +import java.security.KeyPairGenerator; |
| 21 | +import java.security.MessageDigest; |
| 22 | +import java.security.Signature; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import javax.crypto.KeyAgreement; |
| 26 | +import javax.crypto.Mac; |
| 27 | + |
| 28 | +import org.apache.sshd.common.channel.ChannelListener; |
| 29 | +import org.apache.sshd.common.forward.PortForwardingEventListener; |
| 30 | +import org.apache.sshd.common.io.nio2.Nio2ServiceFactory; |
| 31 | +import org.apache.sshd.common.io.nio2.Nio2ServiceFactoryFactory; |
| 32 | +import org.apache.sshd.common.session.SessionListener; |
| 33 | +import org.apache.sshd.common.util.security.bouncycastle.BouncyCastleSecurityProviderRegistrar; |
| 34 | +import org.apache.sshd.common.util.security.eddsa.EdDSASecurityProviderRegistrar; |
| 35 | +import org.eclipse.jgit.api.FetchCommand; |
| 36 | +import org.eclipse.jgit.api.MergeCommand; |
| 37 | +import org.eclipse.jgit.internal.transport.sshd.SshdText; |
| 38 | + |
| 39 | +import org.springframework.aot.hint.MemberCategory; |
| 40 | +import org.springframework.aot.hint.RuntimeHints; |
| 41 | +import org.springframework.aot.hint.RuntimeHintsRegistrar; |
| 42 | +import org.springframework.aot.hint.TypeReference; |
| 43 | +import org.springframework.cloud.config.environment.PropertyValueDescriptor; |
| 44 | +import org.springframework.cloud.config.server.ssh.HostKeyAlgoSupportedValidator; |
| 45 | +import org.springframework.cloud.config.server.ssh.HostKeyAndAlgoBothExistValidator; |
| 46 | +import org.springframework.cloud.config.server.ssh.KnownHostsFileValidator; |
| 47 | +import org.springframework.cloud.config.server.ssh.PrivateKeyValidator; |
| 48 | +import org.springframework.cloud.config.server.ssh.SshPropertyValidator; |
| 49 | +import org.springframework.util.ClassUtils; |
| 50 | + |
| 51 | +/** |
| 52 | + * A {@link RuntimeHintsRegistrar} implementation that makes types required by Config |
| 53 | + * Server available in constrained environments. |
| 54 | + * |
| 55 | + * @author Olga Maciaszek-Sharma |
| 56 | + * @since 4.1.0 |
| 57 | + */ |
| 58 | +class ConfigServerRuntimeHints implements RuntimeHintsRegistrar { |
| 59 | + |
| 60 | + @Override |
| 61 | + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { |
| 62 | + if (!ClassUtils.isPresent("org.springframework.cloud.config.server.config.ConfigServerConfiguration", |
| 63 | + classLoader)) { |
| 64 | + return; |
| 65 | + } |
| 66 | + hints.reflection().registerTypes(Set.of(TypeReference.of(HostKeyAndAlgoBothExistValidator.class), |
| 67 | + TypeReference.of(KnownHostsFileValidator.class), TypeReference.of(HostKeyAlgoSupportedValidator.class), |
| 68 | + TypeReference.of(PrivateKeyValidator.class), TypeReference.of(SshPropertyValidator.class), |
| 69 | + TypeReference.of(PropertyValueDescriptor.class)), |
| 70 | + hint -> hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); |
| 71 | + hints.reflection().registerTypes( |
| 72 | + Set.of(TypeReference.of(PropertyValueDescriptor.class), TypeReference.of(Mac.class), |
| 73 | + TypeReference.of(KeyAgreement.class), TypeReference.of(KeyPairGenerator.class), |
| 74 | + TypeReference.of(KeyFactory.class), TypeReference.of(Signature.class), |
| 75 | + TypeReference.of(MessageDigest.class)), |
| 76 | + hint -> hint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); |
| 77 | + |
| 78 | + // TODO: move over to GraalVM reachability metadata |
| 79 | + if (ClassUtils.isPresent("org.apache.sshd.common.SshConstants", classLoader)) { |
| 80 | + hints.reflection().registerTypes(Set.of(TypeReference.of(BouncyCastleSecurityProviderRegistrar.class), |
| 81 | + TypeReference.of(EdDSASecurityProviderRegistrar.class), TypeReference.of(Nio2ServiceFactory.class), |
| 82 | + TypeReference.of(Nio2ServiceFactoryFactory.class)), |
| 83 | + hint -> hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); |
| 84 | + hints.reflection().registerTypes(Set.of(TypeReference.of(PortForwardingEventListener.class)), |
| 85 | + hint -> hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, |
| 86 | + MemberCategory.INVOKE_DECLARED_METHODS, MemberCategory.DECLARED_FIELDS)); |
| 87 | + hints.proxies().registerJdkProxy(TypeReference.of(ChannelListener.class), |
| 88 | + TypeReference.of(PortForwardingEventListener.class), TypeReference.of(SessionListener.class)); |
| 89 | + } |
| 90 | + |
| 91 | + // TODO: move over to GraalVM reachability metadata |
| 92 | + if (ClassUtils.isPresent("org.eclipse.jgit.api.Git", classLoader)) { |
| 93 | + hints.reflection() |
| 94 | + .registerTypes(Set.of(TypeReference.of(MergeCommand.FastForwardMode.Merge.class), |
| 95 | + TypeReference.of(MergeCommand.ConflictStyle.class), |
| 96 | + TypeReference.of(MergeCommand.FastForwardMode.class), TypeReference.of(FetchCommand.class)), |
| 97 | + hint -> hint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); |
| 98 | + hints.reflection().registerTypes(Set.of(TypeReference.of(SshdText.class)), hint -> hint |
| 99 | + .withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.DECLARED_FIELDS)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments