Skip to content

Commit

Permalink
fix #3102: add authentication cache
Browse files Browse the repository at this point in the history
  • Loading branch information
dgnorton committed Jun 26, 2015
1 parent 5626c2c commit fb328bf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
6 changes: 3 additions & 3 deletions influxql/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ func (*DropRetentionPolicyStatement) node() {}
func (*DropSeriesStatement) node() {}
func (*DropUserStatement) node() {}
func (*GrantStatement) node() {}
func (*RevokeStatement) node() {}
func (*SelectStatement) node() {}
func (*SetPasswordUserStatement) node() {}
func (*ShowContinuousQueriesStatement) node() {}
func (*ShowGrantsForUserStatement) node() {}
func (*ShowServersStatement) node() {}
Expand All @@ -105,9 +108,6 @@ func (*ShowDiagnosticsStatement) node() {}
func (*ShowTagKeysStatement) node() {}
func (*ShowTagValuesStatement) node() {}
func (*ShowUsersStatement) node() {}
func (*RevokeStatement) node() {}
func (*SelectStatement) node() {}
func (*SetPasswordUserStatement) node() {}

func (*BinaryExpr) node() {}
func (*BooleanLiteral) node() {}
Expand Down
17 changes: 6 additions & 11 deletions influxql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (p *Parser) ParseStatement() (Statement, error) {
case ALTER:
return p.parseAlterStatement()
case SET:
return p.parseSetStatement()
return p.parseSetPasswordUserStatement()
default:
return nil, newParseError(tokstr(tok, lit), []string{"SELECT", "DELETE", "SHOW", "CREATE", "DROP", "GRANT", "REVOKE", "ALTER", "SET"}, pos)
}
Expand Down Expand Up @@ -207,19 +207,14 @@ func (p *Parser) parseAlterStatement() (Statement, error) {
return nil, newParseError(tokstr(tok, lit), []string{"RETENTION"}, pos)
}

// parseSetStatement parses a string and returns a set statement.
// parseSetPasswordUserStatement parses a string and returns a set statement.
// This function assumes the SET token has already been consumed.
func (p *Parser) parseSetStatement() (*SetPasswordUserStatement, error) {
func (p *Parser) parseSetPasswordUserStatement() (*SetPasswordUserStatement, error) {
stmt := &SetPasswordUserStatement{}

// Consume the required PASSWORD token.
if tok, pos, lit := p.scanIgnoreWhitespace(); tok != PASSWORD {
return nil, newParseError(tokstr(tok, lit), []string{"PASSWORD"}, pos)
}

// Consume the required FOR token.
if tok, pos, lit := p.scanIgnoreWhitespace(); tok != FOR {
return nil, newParseError(tokstr(tok, lit), []string{"FOR"}, pos)
// Consume the required PASSWORD FOR tokens.
if err := p.parseTokens([]Token{PASSWORD, FOR}); err != nil {
return nil, err
}

// Parse username
Expand Down
14 changes: 14 additions & 0 deletions meta/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ type Store struct {
// The amount of time without an apply before sending a heartbeat.
CommitTimeout time.Duration

// Authentication cache.
authCache map[string]string

Logger *log.Logger
}

Expand All @@ -116,6 +119,7 @@ func NewStore(c Config) *Store {
ElectionTimeout: time.Duration(c.ElectionTimeout),
LeaderLeaseTimeout: time.Duration(c.LeaderLeaseTimeout),
CommitTimeout: time.Duration(c.CommitTimeout),
authCache: make(map[string]string, 0),
Logger: log.New(os.Stderr, "", log.LstdFlags),
}
}
Expand Down Expand Up @@ -980,11 +984,19 @@ func (s *Store) Authenticate(username, password string) (ui *UserInfo, err error
return ErrUserNotFound
}

// Check the local auth cache first.
if p, ok := s.authCache[username]; ok && p == password {
ui = u
return nil
}

// Compare password with user hash.
if err := bcrypt.CompareHashAndPassword([]byte(u.Hash), []byte(password)); err != nil {
return err
}

s.authCache[username] = password

ui = u
return nil
})
Expand Down Expand Up @@ -1564,6 +1576,7 @@ func (fsm *storeFSM) applyDropUserCommand(cmd *internal.Command) interface{} {
return err
}
fsm.data = other
delete(fsm.authCache, v.GetName())
return nil
}

Expand All @@ -1577,6 +1590,7 @@ func (fsm *storeFSM) applyUpdateUserCommand(cmd *internal.Command) interface{} {
return err
}
fsm.data = other
delete(fsm.authCache, v.GetName())
return nil
}

Expand Down

0 comments on commit fb328bf

Please sign in to comment.