Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error refreshing state #37

Open
aellwein opened this issue Apr 10, 2023 · 4 comments
Open

Error refreshing state #37

aellwein opened this issue Apr 10, 2023 · 4 comments

Comments

@aellwein
Copy link

aellwein commented Apr 10, 2023

Hi,
i am trying out terraform-backend-git v0.1.4 on a M1 Mac.

For the backend, i've created a script on PATHcalled tfbg with following content:

#!/bin/sh
ssh-agent  # this is probably not needed
terraform-backend-git -l git terraform "$@"

In a terraform folder in my repo, i've created a terraform-backend-git.hcl file with following content:

git.repository = "ssh://CEPBEPOK/~alex/git/infra_state.git"
git.ref = "main"
git.state = "/tfstate.json"

Upon using tfbg init, i get a git_http_backend_auto.tf file created:

terraform {
	backend "http" {
		address = "http://localhost:6061/?type=git&repository=ssh://CEPBEPOK/~alex/git/infra_state.git&ref=main&state=/tfstate.json"
		lock_address = "http://localhost:6061/?type=git&repository=ssh://CEPBEPOK/~alex/git/infra_state.git&ref=main&state=/tfstate.json"
		unlock_address = "http://localhost:6061/?type=git&repository=ssh://CEPBEPOK/~alex/git/infra_state.git&ref=main&state=/tfstate.json"
		skip_cert_verification = false
		username = ""
		password = ""
	}
}

But the command execution leaves me with following:

[terraform-backend-git]: Using config file: /Users/alex/git/infra/terraform/terraform-backend-git.hcl
[terraform-backend-git]: WARNING: HTTP basic auth is disabled, please specify TF_BACKEND_GIT_HTTP_USERNAME and TF_BACKEND_GIT_HTTP_PASSWORD
[terraform-backend-git]: listen on 127.0.0.1:6061

Initializing the backend...
[terraform-backend-git]: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
[terraform-backend-git]: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
[terraform-backend-git]: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
Error refreshing state: Failed to get state: GET http://localhost:6061/?type=git&repository=ssh://CEPBEPOK/~alex/git/infra_state.git&ref=main&state=/tfstate.json giving up after 3 attempt(s)
Error: exit status 1

Looks like something is going wrong on the SSH level.
Cloning the infra_state.git repo from the command line works just fine (BTW it's a bare repository).

P.S.: Maybe worth mentioning: the CEPBEPOK hostname is not directly resolvable; it's defined in some ~/.ssh/config file of mine (it's a server located on my local network). As being said, the git clone works without any issues.

@aellwein
Copy link
Author

aellwein commented Apr 10, 2023

After some tweaks on mentionedtfbg script and founding out that the default configuration of Mac's ssh-agent is pretty weird, i've ended up with this script:

#!/bin/sh
set -eu

ssh-add --apple-use-keychain ~/.ssh/id_ecdsa
export SSH_PRIVATE_KEY=/Users/alex/.ssh/id_ecdsa

echo "Using private key: $SSH_PRIVATE_KEY"
echo "Auth socket: $SSH_AUTH_SOCK"
terraform-backend-git -l git terraform "$@"

but this gives another error output:

tfbg init -reconfigure
Identity added: /Users/alex/.ssh/id_ecdsa (alex@xyz)
Using private key: /Users/alex/.ssh/id_ecdsa
Auth socket: /var/folders/40/mjxwzzd163b2h6jthg5hh9lw0000gn/T//ssh-a5J6lJHKktCT/agent.41225
[terraform-backend-git]: Using config file: /Users/alex/git/infra/terraform/terraform-backend-git.hcl
[terraform-backend-git]: WARNING: HTTP basic auth is disabled, please specify TF_BACKEND_GIT_HTTP_USERNAME and TF_BACKEND_GIT_HTTP_PASSWORD
[terraform-backend-git]: WARNING: Access Logs enabled
[terraform-backend-git]: listen on 127.0.0.1:6061

Initializing the backend...

Successfully configured the backend "http"! Terraform will automatically
use this backend unless the backend configuration changes.
[terraform-backend-git]: repository not found
127.0.0.1 - - [10/Apr/2023:18:10:08 +0200] "GET /?type=git&repository=ssh://CEPBEPOK/~alex/git/infra_state.git&ref=main&state=./tfstate.json HTTP/1.1" 500 27
[terraform-backend-git]: repository not found
127.0.0.1 - - [10/Apr/2023:18:10:10 +0200] "GET /?type=git&repository=ssh://CEPBEPOK/~alex/git/infra_state.git&ref=main&state=./tfstate.json HTTP/1.1" 500 27
[terraform-backend-git]: repository not found
127.0.0.1 - - [10/Apr/2023:18:10:13 +0200] "GET /?type=git&repository=ssh://CEPBEPOK/~alex/git/infra_state.git&ref=main&state=./tfstate.json HTTP/1.1" 500 27
Error refreshing state: Failed to get state: GET http://localhost:6061/?type=git&repository=ssh://CEPBEPOK/~alex/git/infra_state.git&ref=main&state=./tfstate.json giving up after 3 attempt(s)
Error: exit status 1

@aellwein
Copy link
Author

@dee-kryvenko any suggestions here?

@dee-kryvenko
Copy link
Member

dee-kryvenko commented Apr 26, 2023

I am not immediately sure, I have not tried it with self-hosted Git repos yet. Can you try export StrictHostKeyChecking=no on the off chance it has something to do with the host key validation and the error message is off? If that doesn't help, I'm gonna need a bit more details (like - how is your Git server is set up) to try to reproduce it locally, although I am a little swamped at work at the moment so it will have to wait for a bit longer.

@aellwein
Copy link
Author

aellwein commented May 7, 2023

Unfortunately, setting "StrictHostKeyChecking" doesn't change anything. I will try to find some time to debug into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants