-
Notifications
You must be signed in to change notification settings - Fork 7
Cleanup domain policy #245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sydneyli
wants to merge
13
commits into
master
Choose a base branch
from
cleanup-domain-policy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a9cc697
Move domains functions to a separate file
sydneyli d847556
s/Domain/PolicySubmission
sydneyli 8ca7acb
Create new policy model abstraction from domain model
sydneyli 61f2c69
Test new model functions
sydneyli 846da9c
Plumbing and test migrations for database access to new domain model
sydneyli f8e1a14
Replace Domain usage with Policy abstraction
sydneyli 6ab6609
fix HostnamesForDomain functionality and test
sydneyli 3a40121
Fix remaining tests and lint
sydneyli 1316c64
Disambiguate between database error vs. no entry found
sydneyli 5e3cf87
Test more cases in models/policy
sydneyli 1944d5a
Address comments
sydneyli 7c6bc4a
Fix DB tests
sydneyli 1ed0381
Merge branch 'master' into cleanup-domain-policy
sydneyli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # Database structure |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package db | ||
|
|
||
| import ( | ||
| "database/sql" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/EFForg/starttls-backend/models" | ||
| "github.com/EFForg/starttls-backend/policy" | ||
| ) | ||
|
|
||
| // PolicyDB is a database of PolicySubmissions. | ||
| type PolicyDB struct { | ||
| tableName string | ||
| conn *sql.DB | ||
| strict bool | ||
| } | ||
|
|
||
| func (p *PolicyDB) formQuery(query string) string { | ||
| return fmt.Sprintf(query, p.tableName, "domain, email, mta_sts, mxs, mode") | ||
| } | ||
|
|
||
| type scanner interface { | ||
| Scan(dest ...interface{}) error | ||
| } | ||
|
|
||
| func (p *PolicyDB) scanPolicy(result scanner) (models.PolicySubmission, error) { | ||
| data := models.PolicySubmission{Policy: new(policy.TLSPolicy)} | ||
| var rawMXs string | ||
| err := result.Scan( | ||
| &data.Name, &data.Email, | ||
| &data.MTASTS, &rawMXs, &data.Policy.Mode) | ||
| data.Policy.MXs = strings.Split(rawMXs, ",") | ||
| return data, err | ||
| } | ||
|
|
||
| // GetPolicies returns a list of policy submissions that match | ||
| // the mtasts status given. | ||
| func (p *PolicyDB) GetPolicies(mtasts bool) ([]models.PolicySubmission, error) { | ||
| rows, err := p.conn.Query(p.formQuery( | ||
| "SELECT %[2]s FROM %[1]s WHERE mta_sts=$1"), mtasts) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer rows.Close() | ||
| policies := []models.PolicySubmission{} | ||
| for rows.Next() { | ||
| policy, err := p.scanPolicy(rows) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| policies = append(policies, policy) | ||
| } | ||
| return policies, nil | ||
| } | ||
|
|
||
| // GetPolicy returns the policy submission for the given domain. | ||
| // Returns the submission (if found), whether it was found, and any errors encountered. | ||
| func (p *PolicyDB) GetPolicy(domainName string) (policy models.PolicySubmission, ok bool, err error) { | ||
| row := p.conn.QueryRow(p.formQuery( | ||
| "SELECT %[2]s FROM %[1]s WHERE domain=$1"), domainName) | ||
| result, err := p.scanPolicy(row) | ||
| if err == sql.ErrNoRows { | ||
| return result, false, nil | ||
| } | ||
| return result, true, err | ||
| } | ||
|
|
||
| // RemovePolicy removes the policy submission with the given domain from | ||
| // the database. | ||
| func (p *PolicyDB) RemovePolicy(domainName string) (models.PolicySubmission, error) { | ||
| row := p.conn.QueryRow(p.formQuery( | ||
| "DELETE FROM %[1]s WHERE domain=$1 RETURNING %[2]s"), domainName) | ||
| return p.scanPolicy(row) | ||
| } | ||
|
|
||
| // PutOrUpdatePolicy upserts the given policy into the data store, if | ||
| // CanUpdate passes. | ||
| func (p *PolicyDB) PutOrUpdatePolicy(ps *models.PolicySubmission) error { | ||
| if p.strict && !ps.CanUpdate(p) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like these guards - this seems like a good place for this type of validation. It seems like we're never running this code with |
||
| return fmt.Errorf("can't update policy in restricted table") | ||
| } | ||
| if p.strict && ps.Policy == nil { | ||
| return fmt.Errorf("can't degrade policy in restricted table") | ||
| } | ||
| if ps.Policy == nil { | ||
| ps.Policy = &policy.TLSPolicy{MXs: []string{}, Mode: ""} | ||
| } | ||
| _, err := p.conn.Exec(p.formQuery( | ||
| "INSERT INTO %[1]s(%[2]s) VALUES($1, $2, $3, $4, $5) "+ | ||
| "ON CONFLICT (domain) DO UPDATE SET "+ | ||
| "email=$2, mta_sts=$3, mxs=$4, mode=$5"), | ||
| ps.Name, ps.Email, ps.MTASTS, | ||
| strings.Join(ps.Policy.MXs[:], ","), ps.Policy.Mode) | ||
| return err | ||
| } | ||
|
|
||
| // DomainsToValidate [interface Validator] retrieves domains from the | ||
| // DB whose policies should be validated-- all Pending policies. | ||
| func (db SQLDatabase) DomainsToValidate() ([]string, error) { | ||
| domains := []string{} | ||
| data, err := db.PendingPolicies.GetPolicies(false) | ||
| if err != nil { | ||
| return domains, err | ||
| } | ||
| for _, domainInfo := range data { | ||
| domains = append(domains, domainInfo.Name) | ||
| } | ||
| return domains, nil | ||
| } | ||
|
|
||
| // HostnamesForDomain [interface Validator] retrieves the hostname policy for | ||
| // a particular domain in Pending. | ||
| func (db SQLDatabase) HostnamesForDomain(domain string) ([]string, error) { | ||
| data, ok, err := db.PendingPolicies.GetPolicy(domain) | ||
| if !ok { | ||
| err = fmt.Errorf("domain %s not in database", domain) | ||
| } | ||
| if err != nil { | ||
| return []string{}, err | ||
| } | ||
| return data.Policy.MXs, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use https://golang.org/pkg/database/sql/#Scanner?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite, I think-- It looks like
Scannerexpects a single interface{} destination, whereas here we are scanning into a varied number:...interface{}