Skip to content

Commit

Permalink
Merge pull request #4 from aurimasl/feature/revoke_more_perms
Browse files Browse the repository at this point in the history
Revoke CREATE, INDEX
  • Loading branch information
aurimasl authored Apr 18, 2018
2 parents 6ba679f + 6fbec09 commit 53a265e
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (
).Default("/var/lib/mysql").Short('p').String()
limit = kingpin.Flag(
"limit", "DB limit in MB.",
).Default("3072").Short('l').Uint64()
).Default("3174").Short('l').Uint64()
configMycnf = kingpin.Flag(
"config",
"Path to .my.cnf file to read MySQL credentials from.",
Expand Down Expand Up @@ -123,7 +123,13 @@ func main() {
if isUserDb(dirName) {
if time.Since(dbs[dirName].lastCheckAt) > 5*time.Second {
size := dirSize(filepath.Dir(event.Name))
if size > *limit {
if size > *limit && size > dbs[dirName].size {
log.Warnf(
"%s exceeded limit of %s with it's %s. I must keep it fit.",
dirName,
humanize.IBytes(*limit),
humanize.IBytes(size),
)
if *dryRun != true {
revokePermissions(dirName)
}
Expand All @@ -148,7 +154,7 @@ func main() {

// watch for errors
case err := <-watcher.Errors:
log.Errorf("ERROR", "err", err)
log.Errorf("ERROR", err)
case <-c1:
scanDatadir()
}
Expand Down Expand Up @@ -212,15 +218,6 @@ func dirSize(path string) uint64 {
return err
})
log.Debugf("Size of %s is %d", path, size)

if size >= *limit {
log.Warnf(
"%s exceeded limit of %s with it's %s. I must keep it fit.",
filepath.Base(path),
humanize.IBytes(*limit),
humanize.IBytes(size),
)
}
return size
}

Expand All @@ -237,7 +234,7 @@ func revokePermissions(dbName string) {

stmt, err := db.Prepare(
fmt.Sprintf(
"SELECT User, Host FROM mysql.db WHERE Db = '%s' AND (Insert_priv = 'Y' OR Update_priv = 'Y')",
"SELECT User, Host FROM mysql.db WHERE Db = '%s' AND (Insert_priv = 'Y' OR Update_priv = 'Y' OR Create_priv = 'Y' OR Index_priv = 'Y')",
dbName,
),
)
Expand All @@ -257,10 +254,10 @@ func revokePermissions(dbName string) {
if err := rows.Scan(&user, &host); err != nil {
panic(err.Error())
}
log.Infof("REVOKE INSERT, UPDATE ON %s.* FROM '%s'@'%s'", dbName, user, host)
log.Infof("REVOKE INSERT, UPDATE, CREATE, INDEX ON %s.* FROM '%s'@'%s'", dbName, user, host)
_, err = db.Exec(
fmt.Sprintf(
"REVOKE INSERT, UPDATE ON %s.* FROM '%s'@'%s'",
"REVOKE INSERT, UPDATE, CREATE, INDEX ON %s.* FROM '%s'@'%s'",
dbName,
user,
host,
Expand Down

0 comments on commit 53a265e

Please sign in to comment.