views:

60

answers:

4

Context: A Java client-side application needs to access a server-side MySQL database.

Need: Limit the possible number of requests to the database for each client (based on the client's IP).

Question 1: Is it possible to do this just by changing the MySQL database settings?

Question 2: Is it a good idea to allow access to the database directly from the client-side application? Or should I rather make the client-side app communicate with a server-side app by TCP ? (and thus only allowing access to the DB by the server-side app)

+2  A: 
  1. You can limit the access to the db by user and host.
  2. It depends on the sensitivity of the data you are passing and the network you are using (Internet or LAN).
bogdanvursu
**1)** I don't want to create a database user per client. (I'm only using one "client user".) But how can I limit the access by host (IP) ?**2)** I'm passing important user data over the Internet.
asmo
+1  A: 

Client -> Server -> Database is certainly the most secure approach, since Client -> Database requires the client application to contain database Login credentials.

Going with Client -> Server -> Database nullifies question one :].

DRL
You confirmed my thoughts.
asmo
+4  A: 
  1. Yes. Look at the syntax for MySQL's GRANT statement. In the with_option, you can specifiy MAX_QUERIES_PER_HOUR. You can even set limits for an existing user without affecting other privileges:

    GRANT USAGE ON db.* TO ... REQUIRE SSL WITH MAX_QUERIES_PER_HOUR 120 MAX_UPDATES_PER_HOUR 100;

  2. Sure, as long as the clients are on the internal network of the database and are only given the minimal permissions that they need to function properly. It might be somewhat difficult maintaining the MySQL grants for all of the clients, but at least you will be using a proven security system rather than attempting to re-write a security system as a server-side application, which could have bugs.

See also 5.5.4. Setting Account Resource Limits.

Daniel Trebbien
Additionally you can control (via the mysql.user table): max_questions, max_updates, max_connections, max_user_connections
John M
A security system that denies or allows operations on a table level is barely enough for most applications. Every user who is allowed to update his mail address would also be allowed to update all other values from the same table. Same for reads. That isn't acceptable for most applications. A thin webservice layer (e.g. JAX-WS with WSS4J) is far more flexible.
sfussenegger
I have modified my answer to specify explicitly that the clients should be on the internal network, as @extraneon explicitly mentioned. A database must not be exposed to the Internet as it's too much of a security risk.
Daniel Trebbien
@sfussenegger: True. I was presuming that clients would only be executing SELECT and INSERT statements. If UPDATEs are necessary, then the "minimal permissions" clause is broken. Creating views for each client of "their data" would likely be infeasible, so a server-side application would probably be best for facilitating UPDATEs.
Daniel Trebbien
+3  A: 

About Question 2.

I think it depends on the sensitivity of the data, the network connection between the server and the client, and the rollout schema of the clients.

  • Sensitive data: I'd go with a client - server - DB solution for this one for sure. You can then implement https connections with possibly client-side certificates or card readers.
  • Network connection: You don't want to expose a DB to the internet. Ever. But if it's an internal application, only accessible from a protected company network, and the data is not sensitive a direct DB connection is feasible. Do handle row locking or transaction handling correctly otherwise your users won't be happy :)
  • Rollout schema: If the application is actively developed there's a fair chance that the database schema changes with it, forcing clients to update. With an intermediate server speaking, for example, SOAP/XML, you decouple database schema from data requests and you have more room to upgrade/change the database without updating all the clients.

I'd personally go for an intermediate server, as it is way simpler to maintain and optimize. There are good solutions in both Python and Java which allow you to quickly create SOAP/XML servers with database access. You can then define XML messages based on what's needed in a certain screen, with appropriate locks, and no regard (ehm) for the database schema. It decouples the database model from the MVC model in the client and allows the client to be relatively clean.

extraneon