hashicorp / terraform-provider-mysql

Terraform MySQL provider – This Terraform provider is archived per our provider archiving process: https://terraform.io/docs/internals/archiving.html
https://www.terraform.io/docs/providers/mysql/
Mozilla Public License 2.0
61 stars 189 forks source link

GRANT not working on wrong database name #28

Closed sebastianwahn closed 6 years ago

sebastianwahn commented 6 years ago

When creating a grant resource for a user and a database, the grant is not created, failing with "Error 1046: No database selected".

This is due to the fact, that the minus character "-" is not allowed in a GRANT statement (for MariaDB). Therefore i suggest, to check the database name to only allow "_" characters instead of "-".

Currently, the database is created, but the grant can not be given to the user. I would expect that the database is also not created, since the database name is not allowed in all SQL statements.

Terraform Version

0.11.1

Affected Resource(s)

Terraform Configuration Files

mysql_database resource:

resource "mysql_database" "some-database" {
  name = "some-database"
}

mysql_grant resource:

resource "mysql_grant" "some-grant" {
  user = "${mysql_user.some-user.user}"
  host = "${mysql_user.some-user.host}"
  database = "${mysql_database.some-database.name}"
  privileges = [
    "ALTER",
    "CREATE",
    "DELETE",
    "DROP",
    "INDEX",
    "INSERT",
    "LOCK TABLES",
    "SELECT",
    "UPDATE"
  ]
}
resource "mysql_user" "some-user" {
  user = "some-user"
  plaintext_password = "123456"
  host = "%"
}

(shortened) Debug Output

According to the debug log, it will generate following SQL statement:

GRANT LOCK TABLES,DROP,UPDATE,INDEX,CREATE,ALTER,SELECT,DELETE,INSERT on some-database.* TO 'some-user'@'%'

Which is not a valid SQL statement for MariaDB.

Expected Behavior

The GRANT statement is executed properly.

Actual Behavior

terraform apply fails with Error 1046: No database selected

Steps to Reproduce

Let it fail (with above resources):

  1. terraform apply

Let it succeed:

  1. change database to "some_database"
  2. terraform apply

Important Factoids

Running on AWS RDS using MariaDB 10.1.26

moxli commented 6 years ago

I have faced a very similar issue today. My problem was that my database name included a hyphen ("-").

I see that you have your database name also with a - (hyphen), which is not supported by default.

I don't know if you just wrote the hyphen because it is a habit or you database name really includes a hyphen.

I hope this helps you or anyone stumbling over this issue.

nunofernandes commented 6 years ago

You can use backticks in the database name and it works :)

resource "mysql_grant" "some-grant" {
  user = "${mysql_user.some-user.user}"
  host = "${mysql_user.some-user.host}"
  database = "`${mysql_database.some-database.name}`"
  privileges = [
    "ALTER",
    "CREATE",
    "DELETE",
    "DROP",
    "INDEX",
    "INSERT",
    "LOCK TABLES",
    "SELECT",
    "UPDATE"
  ]
}
moxli commented 6 years ago

Thats ridiculous! How is it possible that Terraform doesn't escape database names .__.

sebastianwahn commented 6 years ago

@moxli My database really contains hyphens, at least at this point. I changed it after I faced this issue. @nunofernandes Thanks for the hint of escaping the names.

Isn't the mysql-provider able to escape database names on its own?

ghost commented 6 years ago

@sebastianwahn @moxli I do not have feeling this is anyhow related the core of terraform. This is just how this provider was written. This bug should not be so difficult to be fixed. I will try to have a look at that.

joestump commented 6 years ago

This is a duplicate of #20. I've addressed it in #45. Closing this out for now.