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

Connection errors when using SubString{String} type #203

Closed
Octogonapus opened this issue Oct 24, 2022 · 6 comments
Closed

Connection errors when using SubString{String} type #203

Octogonapus opened this issue Oct 24, 2022 · 6 comments

Comments

@Octogonapus
Copy link
Contributor

I just finished debugging #201 on my end and it turns out that the token problems actually came down to types and the ccalls in this package. If I call DBInterface.connect with a password that is a ::SubString{String}, something bad happens to the password and the server believes it to be incorrect. If I convert it to a ::String, all is good.

I would suggest changing the types on the C API to be String instead of AbstractString. I also haven't done a review for other potential gotchas.

@quinnj
Copy link
Member

quinnj commented Oct 25, 2022

Oh wow, great sleuthing! Hmmm, the result surprises me because I would have thought the ccall conversions would have worked. Let me dig in a bit and see if I can figure out what's going wrong.

@quinnj
Copy link
Member

quinnj commented Oct 25, 2022

Ah shoot, yep, so we can see the problem with the following:

julia> arg = view("  password  ", 3:10)
"password"

julia> ptr = convert(Ptr{Cvoid}, pointer(arg))
Ptr{Nothing} @0x00000001190f2e5a

julia> unsafe_string(convert(Ptr{UInt8}, ptr))
"password  "

so basically, if your SubString has any characters after the last point of the range, they still get included in the ccall convert.

@quinnj
Copy link
Member

quinnj commented Oct 25, 2022

Ok PR up: #204

@Octogonapus
Copy link
Contributor Author

Thanks, though I tried the latest main with my MWE and the changes don't fix the problem. This is my code:

sout_io = IOBuffer()
serr_io = IOBuffer()
run(pipeline(
    `aws rds generate-db-auth-token
        --hostname $(ENV["PROXY_ENDPOINT"])
        --port $(ENV["DB_PORT"])
        --region $(ENV["AWS_DEFAULT_REGION"])
        --username $(ENV["DB_USERNAME"])`,
    stdout=sout_io,
    stderr=serr_io,
))
resp_out = String(take!(sout_io))
resp_err = String(take!(serr_io))
db_auth_token = isempty(resp_out) ? resp_err : resp_out
db_auth_token_stripped = strip(db_auth_token) # must wrap this in String(...) to make the connection work

conn = DBInterface.connect(
    MySQL.Connection,
    ENV["PROXY_ENDPOINT"],
    ENV["DB_USERNAME"],
    db_auth_token_stripped;
    unix_socket="",
    port=parse(Int, string(ENV["DB_PORT"])),
    ssl_ca="/runtime/AmazonRootCA1.pem",
    ssl_verify_server_cert=true,
    ssl_enforce=true
)

@quinnj
Copy link
Member

quinnj commented Oct 25, 2022

ah, I think we have more places where we need to do this; sorry about that. I was still stuck on the mysql_options thing we were looking at previously. PR incoming.

@quinnj
Copy link
Member

quinnj commented Oct 25, 2022

#205

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