spring-cloud / spring-cloud-contract

Support for Consumer Driven Contracts in Spring
https://cloud.spring.io/spring-cloud-contract
Apache License 2.0
720 stars 438 forks source link

Docker Stub Runner Git clone: JSchException: USERAUTH fail #1211

Closed Sam-Kruglov closed 5 years ago

Sam-Kruglov commented 5 years ago

I am trying to run the stubs using docker but it fails to clone the repository.

If I execute git clone git://git@bitbucket.org:repoowner/spring-cloud-contracts.git on the host it succeeds. Here is exactly what I am doing:

#!/usr/bin/env bash

#clear console buffer (actually does it unlike 'clear')
printf "\033c"

#go to the directory where the script is located
cd "$(dirname "$0")"

groupId=com.xxx
artifactId=yyy
version=1
consumerName=mobile
port=8080

# if listing ssh keys fails with code 2 then we assume ssh-agent is not started
# https://stackoverflow.com/a/57351920/6166627
# I noticed if SSH_AGENT_PID is empty, this command can still succeed, so we check using this command
ssh-add -l &>/dev/null
[[ "$?" == 2 ]] && eval `ssh-agent`

#lists all identities and takes the first line. In case none found, the first line would be "The agent has no identities."
if [ "$(ssh-add -l | sed -n 1p)" == 'The agent has no identities.' ]
then
  echo 'no SSH keys found in the ssh-agent!'
  echo 'Please, execute manually "ssh-add ~/.ssh/id_rsa"'
  exit 1
fi

currentBranch=$(git rev-parse --abbrev-ref HEAD)

docker run --rm \
  -u root:root \
  -v ~/.ssh:/root/.ssh \
  -e "STUBRUNNER_REPOSITORY_ROOT=git://git@bitbucket.org:repoowner/spring-cloud-contracts.git" \
  -e "STUBRUNNER_PROPERTIES_GIT_BRANCH=$currentBranch" \
  -e "STUBRUNNER_IDS=$groupId:$artifactId:$version:stubs:$port" \
  -e "STUBRUNNER_STUBS_MODE=REMOTE" \
  -e "STUBRUNNER_STUBS_PER_CONSUMER=true" \
  -e "STUBRUNNER_CONSUMER_NAME=$consumerName" \
  -p "$port:$port" \
  springcloud/spring-cloud-contract-stub-runner:2.1.2.RELEASE

Here is the exception:

2019-09-12 08:37:35.475 ERROR 1 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'triggerController' defined in URL [jar:file:/stub-runner-boot.jar!/BOOT-INF/lib/spring-cloud-contract-stub-runner-2.1.2.RELEASE.jar!/org/springframework/cloud/contract/stubrunner/server/TriggerController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchStubRunner' defined in org.springframework.cloud.contract.stubrunner.spring.StubRunnerConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.contract.stubrunner.BatchStubRunner]: Factory method 'batchStubRunner' threw exception; nested exception is java.lang.IllegalStateException: Exception occurred while cloning repo
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1341) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1187) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) [spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) [spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) [spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) [spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) [spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
        at org.springframework.cloud.contract.stubrunner.server.StubRunnerBoot.main(StubRunnerBoot.java:30) [classes!/:2.1.2.RELEASE]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:47) [stub-runner-boot.jar:2.1.2.RELEASE]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:86) [stub-runner-boot.jar:2.1.2.RELEASE]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [stub-runner-boot.jar:2.1.2.RELEASE]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [stub-runner-boot.jar:2.1.2.RELEASE]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchStubRunner' defined in org.springframework.cloud.contract.stubrunner.spring.StubRunnerConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.contract.stubrunner.BatchStubRunner]: Factory method 'batchStubRunner' threw exception; nested exception is java.lang.IllegalStateException: Exception occurred while cloning repo
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:456) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1251) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1171) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        ... 27 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.contract.stubrunner.BatchStubRunner]: Factory method 'batchStubRunner' threw exception; nested exception is java.lang.IllegalStateException: Exception occurred while cloning repo
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        ... 41 common frames omitted
Caused by: java.lang.IllegalStateException: Exception occurred while cloning repo
        at org.springframework.cloud.contract.stubrunner.GitRepo.cloneProject(GitRepo.java:116) ~[spring-cloud-contract-stub-runner-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.cloud.contract.stubrunner.GitContractsRepo.clonedRepo(ScmStubDownloaderBuilder.java:142) ~[spring-cloud-contract-stub-runner-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.cloud.contract.stubrunner.GitStubDownloader.downloadAndUnpackStubJar(ScmStubDownloaderBuilder.java:193) ~[spring-cloud-contract-stub-runner-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.cloud.contract.stubrunner.CompositeStubDownloader.downloadAndUnpackStubJar(CompositeStubDownloaderBuilder.java:80) ~[spring-cloud-contract-stub-runner-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.cloud.contract.stubrunner.StubRunnerFactory.createStubsFromServiceConfiguration(StubRunnerFactory.java:66) ~[spring-cloud-contract-stub-runner-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.cloud.contract.stubrunner.BatchStubRunnerFactory.buildBatchStubRunner(BatchStubRunnerFactory.java:69) ~[spring-cloud-contract-stub-runner-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.cloud.contract.stubrunner.spring.StubRunnerConfiguration.batchStubRunner(StubRunnerConfiguration.java:84) ~[spring-cloud-contract-stub-runner-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.cloud.contract.stubrunner.spring.StubRunnerConfiguration$$EnhancerBySpringCGLIB$$45ba3f99.CGLIB$batchStubRunner$0(<generated>) ~[spring-cloud-contract-stub-runner-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.cloud.contract.stubrunner.spring.StubRunnerConfiguration$$EnhancerBySpringCGLIB$$45ba3f99$$FastClassBySpringCGLIB$$6d9cb061.invoke(<generated>) ~[spring-cloud-contract-stub-runner-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.cloud.contract.stubrunner.spring.StubRunnerConfiguration$$EnhancerBySpringCGLIB$$45ba3f99.batchStubRunner(<generated>) ~[spring-cloud-contract-stub-runner-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        ... 42 common frames omitted
Caused by: java.lang.IllegalStateException: org.eclipse.jgit.api.errors.TransportException: git@bitbucket.org:repoowner/spring-cloud-contracts.git: USERAUTH fail
        at org.springframework.cloud.contract.stubrunner.GitRepo.cloneToBasedir(GitRepo.java:225) ~[spring-cloud-contract-stub-runner-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.cloud.contract.stubrunner.GitRepo.cloneProject(GitRepo.java:107) ~[spring-cloud-contract-stub-runner-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        ... 58 common frames omitted
Caused by: org.eclipse.jgit.api.errors.TransportException: git@bitbucket.org:repoowner/spring-cloud-contracts.git: USERAUTH fail
        at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:254) ~[org.eclipse.jgit-5.1.3.201810200350-r.jar!/:5.1.3.201810200350-r]
        at org.eclipse.jgit.api.CloneCommand.fetch(CloneCommand.java:306) ~[org.eclipse.jgit-5.1.3.201810200350-r.jar!/:5.1.3.201810200350-r]
        at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:200) ~[org.eclipse.jgit-5.1.3.201810200350-r.jar!/:5.1.3.201810200350-r]
        at org.springframework.cloud.contract.stubrunner.GitRepo.cloneToBasedir(GitRepo.java:214) ~[spring-cloud-contract-stub-runner-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        ... 59 common frames omitted
Caused by: org.eclipse.jgit.errors.TransportException: git@bitbucket.org:repoowner/spring-cloud-contracts.git: USERAUTH fail
        at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:192) ~[org.eclipse.jgit-5.1.3.201810200350-r.jar!/:5.1.3.201810200350-r]
        at org.eclipse.jgit.transport.SshTransport.getSession(SshTransport.java:140) ~[org.eclipse.jgit-5.1.3.201810200350-r.jar!/:5.1.3.201810200350-r]
        at org.eclipse.jgit.transport.TransportGitSsh$SshFetchConnection.<init>(TransportGitSsh.java:280) ~[org.eclipse.jgit-5.1.3.201810200350-r.jar!/:5.1.3.201810200350-r]
        at org.eclipse.jgit.transport.TransportGitSsh.openFetch(TransportGitSsh.java:170) ~[org.eclipse.jgit-5.1.3.201810200350-r.jar!/:5.1.3.201810200350-r]
        at org.eclipse.jgit.transport.FetchProcess.executeImp(FetchProcess.java:137) ~[org.eclipse.jgit-5.1.3.201810200350-r.jar!/:5.1.3.201810200350-r]
        at org.eclipse.jgit.transport.FetchProcess.execute(FetchProcess.java:123) ~[org.eclipse.jgit-5.1.3.201810200350-r.jar!/:5.1.3.201810200350-r]
        at org.eclipse.jgit.transport.Transport.fetch(Transport.java:1271) ~[org.eclipse.jgit-5.1.3.201810200350-r.jar!/:5.1.3.201810200350-r]
        at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:243) ~[org.eclipse.jgit-5.1.3.201810200350-r.jar!/:5.1.3.201810200350-r]
        ... 62 common frames omitted
Caused by: com.jcraft.jsch.JSchException: USERAUTH fail
        at com.jcraft.jsch.UserAuthPublicKey.start(UserAuthPublicKey.java:119) ~[jsch-0.1.54.jar!/:na]
        at com.jcraft.jsch.Session.connect(Session.java:470) ~[jsch-0.1.54.jar!/:na]
        at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:146) ~[org.eclipse.jgit-5.1.3.201810200350-r.jar!/:5.1.3.201810200350-r]
        ... 69 common frames omitted
Sam-Kruglov commented 5 years ago

That issue is present generally in docker, no just in this stub runner. Maybe that problem is not even related to Spring Cloud Contract

dsyer commented 5 years ago

Probably ~/.ssh does not belong to root, right? So that doesn't look like a good strategy for mounting your ssh config.

Sam-Kruglov commented 5 years ago

I have mounted that, look:

-u root:root \
-v ~/.ssh:/root/.ssh \
Sam-Kruglov commented 5 years ago

without that I get a different error, saying that bitbucket.org is an unknown host

dsyer commented 5 years ago

Do you need to use ssh? Maybe https would work?

dsyer commented 5 years ago

BTW you mounted the .ssh directory, but unless you are root in the host, the file ownership will be wrong in the container. AFAIK the container runs as root by default so -u root:root seems redundant.

Sam-Kruglov commented 5 years ago

I am currently trying https. SSH is just less work for me because I already have SSH keys installed.

This may be a solution https://stackoverflow.com/a/56527389/6166627 but I am not sure I understand it completely, will need more time to look at it.

I guess you are right, but I don't think permissions cause the problem. I might try manipulating with it if you're certain. About user, I think that is clearer that way because I cannot use ~

Sam-Kruglov commented 5 years ago

HTTPS worked

Sam-Kruglov commented 5 years ago

I would still like to resolve this though. Unless you guys think it's off-topic, which would be understandable