auth

Paddy 2015-03-21 Parent:06fb735031bb Child:379702564771

149:8267e1c8bcd1 Go to Latest

auth/profile_postgres.go

Test our Postgres profileStore implementation. Update all our test cases to use time.Now().Round(time.Millisecond), because Go uses nanosecond precision on time values, but Postgres silently truncates that to millisecond precision. This caused our tests to report false failures that were just silent precision loss, not actual failures. Set up our authd server to use the Postgres store for profiles and automatically create a test scope when starting up. Log errors when creating Clients through the API, instead of just swallowing them and sending back cryptic act of god errors. Add a NewPostgres helper that returns a postgres profileStore from a connection string (passed through pq transparently). Add an Empty() bool helper to ProfileChange and BulkProfileChange types, so we can determine if there are any changes we need to act on easily. Log errors when creating Pofiles through the API, instead of just swalloing them and sending back cryptic act of god errors. Remove the ` quotes around field and table names, which are not supported in Postgres. This required adding a few functions/methods to pan. Detect situations where a profile was expected and not found, and return ErrProfileNotFound. Detect pq errors thrown when the profiles_pkey constraint is violated, and transform them to the ErrProfileAlreadyExists error. Detect empty ProfileChange and BulkProfileChange variables and abort the updateProfile and updateProfiles methods early, before invalid SQL is generated. Detect pq errors thrown when the logins_pkey constraint is violated, and transform them to the ErrLoginAlreadyExists error. Detect when removing a Login and no rows were affected, and return an ErrLoginNotFound. Create an sql dir with a postgres_init script that will initialize the schema of the tables expected in the database.

History
     1.1 --- a/profile_postgres.go	Sat Mar 21 01:23:33 2015 -0400
     1.2 +++ b/profile_postgres.go	Sat Mar 21 14:53:15 2015 -0400
     1.3 @@ -4,6 +4,7 @@
     1.4  	"time"
     1.5  
     1.6  	"code.secondbit.org/uuid.hg"
     1.7 +	"github.com/lib/pq"
     1.8  	"github.com/secondbit/pan"
     1.9  )
    1.10  
    1.11 @@ -17,10 +18,10 @@
    1.12  
    1.13  func (p *postgres) getProfileByIDSQL(id uuid.ID) *pan.Query {
    1.14  	var profile Profile
    1.15 -	fields, _ := pan.GetQuotedFields(profile)
    1.16 +	fields, _ := pan.GetFields(profile)
    1.17  	query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(profile))
    1.18  	query.IncludeWhere()
    1.19 -	query.Include(pan.GetColumn(profile, "ID")+" = ? AND "+pan.GetColumn(profile, "Deleted")+" = ?", id, false)
    1.20 +	query.Include(pan.GetUnquotedColumn(profile, "ID")+" = ? AND "+pan.GetUnquotedColumn(profile, "Deleted")+" = ?", id, false)
    1.21  	return query.FlushExpressions(" ")
    1.22  }
    1.23  
    1.24 @@ -31,27 +32,32 @@
    1.25  		return Profile{}, err
    1.26  	}
    1.27  	var profile Profile
    1.28 +	var found bool
    1.29  	for rows.Next() {
    1.30  		err := pan.Unmarshal(rows, &profile)
    1.31  		if err != nil {
    1.32  			return Profile{}, err
    1.33  		}
    1.34 +		found = true
    1.35  	}
    1.36  	if err := rows.Err(); err != nil {
    1.37  		return Profile{}, err
    1.38  	}
    1.39 +	if !found {
    1.40 +		return profile, ErrProfileNotFound
    1.41 +	}
    1.42  	return profile, nil
    1.43  }
    1.44  
    1.45  func (p *postgres) getProfileByLoginSQL(value string) *pan.Query {
    1.46  	var profile Profile
    1.47  	var login Login
    1.48 -	fields, _ := pan.GetAbsoluteFields(profile)
    1.49 +	fields, _ := pan.GetUnquotedAbsoluteFields(profile)
    1.50  	query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(profile))
    1.51  	query.Include("INNER JOIN " + pan.GetTableName(login))
    1.52 -	query.Include("ON " + pan.GetAbsoluteColumnName(profile, "ID") + " = " + pan.GetAbsoluteColumnName(login, "ProfileID"))
    1.53 +	query.Include("ON " + pan.GetUnquotedAbsoluteColumn(profile, "ID") + " = " + pan.GetUnquotedAbsoluteColumn(login, "ProfileID"))
    1.54  	query.IncludeWhere()
    1.55 -	query.Include(pan.GetAbsoluteColumnName(login, "Value")+" = ? AND "+pan.GetAbsoluteColumnName(profile, "Deleted")+" = ?", value, false)
    1.56 +	query.Include(pan.GetUnquotedAbsoluteColumn(login, "Value")+" = ? AND "+pan.GetUnquotedAbsoluteColumn(profile, "Deleted")+" = ?", value, false)
    1.57  	return query.FlushExpressions(" ")
    1.58  }
    1.59  
    1.60 @@ -62,20 +68,25 @@
    1.61  		return Profile{}, err
    1.62  	}
    1.63  	var profile Profile
    1.64 +	var found bool
    1.65  	for rows.Next() {
    1.66  		err := pan.Unmarshal(rows, &profile)
    1.67  		if err != nil {
    1.68  			return Profile{}, err
    1.69  		}
    1.70 +		found = true
    1.71  	}
    1.72  	if err := rows.Err(); err != nil {
    1.73  		return Profile{}, err
    1.74  	}
    1.75 +	if !found {
    1.76 +		return profile, ErrProfileNotFound
    1.77 +	}
    1.78  	return profile, nil
    1.79  }
    1.80  
    1.81  func (p *postgres) saveProfileSQL(profile Profile) *pan.Query {
    1.82 -	fields, values := pan.GetQuotedFields(profile)
    1.83 +	fields, values := pan.GetFields(profile)
    1.84  	query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(profile))
    1.85  	query.Include("(" + pan.QueryList(fields) + ")")
    1.86  	query.Include("VALUES")
    1.87 @@ -86,46 +97,55 @@
    1.88  func (p *postgres) saveProfile(profile Profile) error {
    1.89  	query := p.saveProfileSQL(profile)
    1.90  	_, err := p.db.Exec(query.String(), query.Args...)
    1.91 +	if e, ok := err.(*pq.Error); ok && e.Constraint == "profiles_pkey" {
    1.92 +		err = ErrProfileAlreadyExists
    1.93 +	}
    1.94  	return err
    1.95  }
    1.96  
    1.97  func (p *postgres) updateProfileSQL(id uuid.ID, change ProfileChange) *pan.Query {
    1.98  	var profile Profile
    1.99  	query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(profile)+" SET ")
   1.100 -	query.IncludeIfNotNil(pan.GetColumn(profile, "Name")+" = ?", change.Name)
   1.101 -	query.IncludeIfNotNil(pan.GetColumn(profile, "Passphrase")+" = ?", change.Passphrase)
   1.102 -	query.IncludeIfNotNil(pan.GetColumn(profile, "Iterations")+" = ?", change.Iterations)
   1.103 -	query.IncludeIfNotNil(pan.GetColumn(profile, "Salt")+" = ?", change.Salt)
   1.104 -	query.IncludeIfNotNil(pan.GetColumn(profile, "PassphraseScheme")+" = ?", change.PassphraseScheme)
   1.105 -	query.IncludeIfNotNil(pan.GetColumn(profile, "Compromised")+" = ?", change.Compromised)
   1.106 -	query.IncludeIfNotNil(pan.GetColumn(profile, "LockedUntil")+" = ?", change.LockedUntil)
   1.107 -	query.IncludeIfNotNil(pan.GetColumn(profile, "PassphraseReset")+" = ?", change.PassphraseReset)
   1.108 -	query.IncludeIfNotNil(pan.GetColumn(profile, "PassphraseResetCreated")+" = ?", change.PassphraseResetCreated)
   1.109 -	query.IncludeIfNotNil(pan.GetColumn(profile, "LastSeen")+" = ?", change.LastSeen)
   1.110 -	query.IncludeIfNotNil(pan.GetColumn(profile, "Deleted")+" = ?", change.Deleted)
   1.111 +	query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Name")+" = ?", change.Name)
   1.112 +	query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Passphrase")+" = ?", change.Passphrase)
   1.113 +	query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Iterations")+" = ?", change.Iterations)
   1.114 +	query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Salt")+" = ?", change.Salt)
   1.115 +	query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "PassphraseScheme")+" = ?", change.PassphraseScheme)
   1.116 +	query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Compromised")+" = ?", change.Compromised)
   1.117 +	query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "LockedUntil")+" = ?", change.LockedUntil)
   1.118 +	query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "PassphraseReset")+" = ?", change.PassphraseReset)
   1.119 +	query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "PassphraseResetCreated")+" = ?", change.PassphraseResetCreated)
   1.120 +	query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "LastSeen")+" = ?", change.LastSeen)
   1.121 +	query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Deleted")+" = ?", change.Deleted)
   1.122  	query.FlushExpressions(", ")
   1.123  	query.IncludeWhere()
   1.124 -	query.Include(pan.GetColumn(profile, "ID")+"= ?", id)
   1.125 +	query.Include(pan.GetUnquotedColumn(profile, "ID")+"= ?", id)
   1.126  	return query.FlushExpressions(" ")
   1.127  }
   1.128  
   1.129  func (p *postgres) updateProfile(id uuid.ID, change ProfileChange) error {
   1.130 +	if change.Empty() {
   1.131 +		return nil
   1.132 +	}
   1.133  	query := p.updateProfileSQL(id, change)
   1.134  	_, err := p.db.Exec(query.String(), query.Args...)
   1.135  	return err
   1.136  }
   1.137  
   1.138  func (p *postgres) updateProfilesSQL(ids []uuid.ID, change BulkProfileChange) *pan.Query {
   1.139 +	if change.Empty() {
   1.140 +		return nil
   1.141 +	}
   1.142  	var profile Profile
   1.143  	query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(profile)+" SET ")
   1.144 -	query.IncludeIfNotNil(pan.GetColumn(profile, "Compromised")+" = ?", change.Compromised)
   1.145 +	query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Compromised")+" = ?", change.Compromised)
   1.146  	query.FlushExpressions(", ")
   1.147  	query.IncludeWhere()
   1.148  	intids := make([]interface{}, len(ids))
   1.149  	for pos, id := range ids {
   1.150  		intids[pos] = id
   1.151  	}
   1.152 -	query.Include(pan.GetColumn(profile, "ID")+" IN ("+pan.VariableList(len(ids))+")", intids...)
   1.153 +	query.Include(pan.GetUnquotedColumn(profile, "ID")+" IN ("+pan.VariableList(len(ids))+")", intids...)
   1.154  	return query.FlushExpressions(" ")
   1.155  }
   1.156  
   1.157 @@ -136,7 +156,7 @@
   1.158  }
   1.159  
   1.160  func (p *postgres) addLoginSQL(login Login) *pan.Query {
   1.161 -	fields, values := pan.GetQuotedFields(login)
   1.162 +	fields, values := pan.GetFields(login)
   1.163  	query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(login))
   1.164  	query.Include("(" + pan.QueryList(fields) + ")")
   1.165  	query.Include("VALUES")
   1.166 @@ -147,6 +167,9 @@
   1.167  func (p *postgres) addLogin(login Login) error {
   1.168  	query := p.addLoginSQL(login)
   1.169  	_, err := p.db.Exec(query.String(), query.Args...)
   1.170 +	if e, ok := err.(*pq.Error); ok && e.Constraint == "logins_pkey" {
   1.171 +		return ErrLoginAlreadyExists
   1.172 +	}
   1.173  	return err
   1.174  }
   1.175  
   1.176 @@ -154,22 +177,32 @@
   1.177  	var login Login
   1.178  	query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(login))
   1.179  	query.IncludeWhere()
   1.180 -	query.Include(pan.GetColumn(login, "Value")+"= ? AND "+pan.GetColumn(login, "ProfileID")+"= ?", value, profile)
   1.181 +	query.Include(pan.GetUnquotedColumn(login, "Value")+"= ? AND "+pan.GetUnquotedColumn(login, "ProfileID")+"= ?", value, profile)
   1.182  	return query.FlushExpressions(" ")
   1.183  }
   1.184  
   1.185  func (p *postgres) removeLogin(value string, profile uuid.ID) error {
   1.186  	query := p.removeLoginSQL(value, profile)
   1.187 -	_, err := p.db.Exec(query.String(), query.Args...)
   1.188 -	return err
   1.189 +	res, err := p.db.Exec(query.String(), query.Args...)
   1.190 +	if err != nil {
   1.191 +		return err
   1.192 +	}
   1.193 +	rows, err := res.RowsAffected()
   1.194 +	if err != nil {
   1.195 +		return err
   1.196 +	}
   1.197 +	if rows == 0 {
   1.198 +		return ErrLoginNotFound
   1.199 +	}
   1.200 +	return nil
   1.201  }
   1.202  
   1.203  func (p *postgres) recordLoginUseSQL(value string, when time.Time) *pan.Query {
   1.204  	var login Login
   1.205  	query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(login)+" SET ")
   1.206 -	query.Include(pan.GetColumn(login, "LastUsed")+"= ?", when)
   1.207 +	query.Include(pan.GetUnquotedColumn(login, "LastUsed")+"= ?", when)
   1.208  	query.IncludeWhere()
   1.209 -	query.Include(pan.GetColumn(login, "Value")+"= ?", value)
   1.210 +	query.Include(pan.GetUnquotedColumn(login, "Value")+"= ?", value)
   1.211  	return query.FlushExpressions(" ")
   1.212  }
   1.213  
   1.214 @@ -181,10 +214,10 @@
   1.215  
   1.216  func (p *postgres) listLoginsSQL(profile uuid.ID, num, offset int) *pan.Query {
   1.217  	var login Login
   1.218 -	fields, _ := pan.GetQuotedFields(login)
   1.219 +	fields, _ := pan.GetFields(login)
   1.220  	query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(login))
   1.221  	query.IncludeWhere()
   1.222 -	query.Include(pan.GetColumn(login, "ProfileID")+"= ?", profile)
   1.223 +	query.Include(pan.GetUnquotedColumn(login, "ProfileID")+"= ?", profile)
   1.224  	query.IncludeLimit(int64(num))
   1.225  	query.IncludeOffset(int64(offset))
   1.226  	return query.FlushExpressions(" ")