0

Having a problem I'm bashing my head at - I have a GET request with the Crystal HTTP client, which runs on my machine, and also on the compiling container - crystallang/crystal, gets a good response and does its job.

But moving the same binary to any other environment (alpine / ubuntu) it just won't, and the exception doesn't make sense to me; I'm getting a weird "No address found for api.example.com:443 over TCP (Socket::Error)" Can't figure what I'm doing wrong. Tried adding ca-certificates and even copying the entire ssl dir to my container. No luck... Here's the code:

def createTag(vtag)
  if vtag.key.empty? || vtag.application.empty? || vtag.subsystem.empty? || vtag.name.empty?
    raise PluginException.new("One of the parameters are empty")
  end

  if vtag.timestamp.empty?
    vtag.timestamp = Time.now.to_s("%Y-%m-%d:%H:%M:%S")
  end

  params = "/api/v1/addTag?key=#{vtag.key}&application=#{vtag.application}&subsystem=#{vtag.subsystem}&name=#{vtag.name}&timestamp=#{vtag.timestamp}"

  response = HTTP::Client.new("api.coralogix.com", tls: true).get(params)

  puts response.body
  puts response.status_code
end

This is the error running the compiled binary on ubuntu:

No address found for api.coralogix.com:443 over TCP (Socket::Error)
  from usr/share/crystal/src/string.cr:4193:13 in 'socket'
  from usr/share/crystal/src/http/client.cr:500:19 in 'exec_internal_single'
  from usr/share/crystal/src/http/client.cr:486:16 in '???'
  from usr/share/crystal/src/crystal/main.cr:0:3 in 'main'
  from ???
  from ???
  from ???
Omer H
  • 415
  • 5
  • 14
  • Have you ensured that `api.coralogix.com` resolves in the failing environment and that this not an issue with Crystal? – Johannes Müller Apr 11 '18 at 08:49
  • The error message is actually not weird but very specific. The host can't be resolved. That has nothing to do with SSL configuration, so your remedies surely wouldn't help. – Johannes Müller Apr 11 '18 at 08:51
  • Guys, this code WORKS! Works locally, and works compiled! Only when moved to a different container architecture I get this! Regardless of the container hosting machine I tried many, locally and on the cloud too. – Omer H Apr 11 '18 at 09:06
  • And YES - the hostname and the port resolves successfully on the new environment too, checked that very specifically! – Omer H Apr 11 '18 at 09:07
  • Please don't mind me asking essential details missing from the question ;) The HTTP client uses `Socket::Addrinfo.resolve` which basically just wraps `getattrinfo` from libc. For debugging this it could probably be useful to see what happens when you resolve in each of these intermediaries. – Johannes Müller Apr 11 '18 at 09:53

1 Answers1

0

Use HTTP:Client.get

def createTag(vtag)
  if vtag.key.empty? || vtag.application.empty? || vtag.subsystem.empty? || vtag.name.empty?
    raise PluginException.new("One of the parameters are empty")
  end

  if vtag.timestamp.empty?
    vtag.timestamp = Time.now.to_s("%Y-%m-%d:%H:%M:%S")
  end

  params = "api/v1/addTag?key=#{vtag.key}&application=#{vtag.application}&subsystem=#{vtag.subsystem}&name=#{vtag.name}&timestamp=#{vtag.timestamp}"

  response = HTTP::Client.get("https://api.coralogix.com/#{params}")

  puts response.body
  puts response.status_code
end
Faustino Aguilar
  • 823
  • 6
  • 15
  • I started from there, this doesn't help it's the same. For the sake of trying, I did go back to that doing what you proposed. It doesn't change anything. – Omer H Apr 10 '18 at 22:20
  • Are you sure your API is ok?, because I just tested again and seems all is working fine, see my demo: https://i.imgur.com/WlTzZPL.gif – Faustino Aguilar Apr 11 '18 at 01:44
  • Of course, I'm sure, told you - it works locally and on the compiling machine. Please read my problem - it's not the code in specific but the environment it runs on: "moving the same binary to any other environment (alpine / ubuntu) it just won't" – Omer H Apr 11 '18 at 06:52
  • Ok, then, Are you sure you have all dependencies in the new environment? Did you try compiling it statically? – Faustino Aguilar Apr 11 '18 at 11:55
  • Yeah sure, It's compiled with --static. – Omer H Apr 11 '18 at 13:02
  • Oh, then, look like your new enviroment have "something" blocking/modifying the http response, Are you using some firewall in yout new enviroment? – Faustino Aguilar Apr 22 '18 at 23:16
  • Nope, same environment. Different container (docker "FROM scratch") I guess it might have something to do with these warning when using `--static`: `"warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking"` – Omer H Apr 23 '18 at 15:03
  • Oh, your should try musl then, see: https://store.docker.com/community/images/jrei/crystal-alpine – Faustino Aguilar Apr 23 '18 at 23:04
  • Or try executing the dynamic linked executable, just copy the right libraries or install the dependencies. – Faustino Aguilar Apr 23 '18 at 23:05
  • Also see: https://manas.tech/blog/2017/04/03/shipping-crystal-apps-in-a-small-docker-image.html – Faustino Aguilar Apr 23 '18 at 23:05
  • 1
    Thanks! Will check all of these out – Omer H Apr 25 '18 at 13:55