auth

Paddy 2015-03-22 Parent:8267e1c8bcd1 Child:73e12d5a1124

151:77db7c65216c Go to Latest

auth/client.go

Implement postgres clientStore. Stop requiring the client ID be passed to clientStore.addEndpoints and context.AddEndpoints. The Endpoints themselves contain the client ID. When using the authd server, set the log flags to include the file path and line number. Add an ErrEndpointAlreadyExists error, to return when creating an endpoint and its ID already exists in the database. Add a Deleted property to Clients and remove the clientStore.deleteClient and context.DeleteClient methods. We're not going to actually remove that data, and we want to be able to restore it, so include it in the ClientChange type and call it using UpdateClient. Create a ClientChange.Empty helper method that will return whether the ClientChange has any changes to perform. Return ErrClientNotFound from clientStore.getClient if the Client's Deleted property is set to true. This also requires us to ignore ErrClientNotFound errors when calling memstore.listClientsByOwner, as they should just be skipped instead of returning an error. Add the postgres type methods needed to implement clientStore. Include postgres as a clientStore if the testing.Short() flag is not set. Generate a new ID for the Client on every run in the tests, now that we can't actually remove it from the database/memstore in code. We really just need a *Store.Reset() function that erases all the data and starts over again, to give the tests a clean execution environment (and so they can clean up after themselves). Add the CREATE TABLE statements for the Clients table and the Endpoints table to sql/postgres_init.sql.

History
     1.1 --- a/client.go	Sat Mar 21 18:46:57 2015 -0400
     1.2 +++ b/client.go	Sun Mar 22 16:26:37 2015 -0400
     1.3 @@ -39,6 +39,9 @@
     1.4  	ErrClientAlreadyExists = errors.New("client already exists in clientStore")
     1.5  	// ErrEndpointNotFound is returned when an Endpoint is requested but not found in a clientSTore.
     1.6  	ErrEndpointNotFound = errors.New("endpoint not found in clientStore")
     1.7 +	// ErrEndpointAlreadyExists is returned when an Endpoint is added to a clientStore, but another Endpoint
     1.8 +	// with the same ID already exists in the clientStore.
     1.9 +	ErrEndpointAlreadyExists = errors.New("endpoint already exists in clientStore")
    1.10  
    1.11  	// ErrEmptyChange is returned when a Change has all its properties set to nil.
    1.12  	ErrEmptyChange = errors.New("change must have at least one property set")
    1.13 @@ -82,6 +85,7 @@
    1.14  	Logo    string  `json:"logo,omitempty"`
    1.15  	Website string  `json:"website,omitempty"`
    1.16  	Type    string  `json:"type,omitempty"`
    1.17 +	Deleted bool    `json:"deleted,omitempty"`
    1.18  }
    1.19  
    1.20  // ApplyChange applies the properties of the passed
    1.21 @@ -102,6 +106,9 @@
    1.22  	if change.Website != nil {
    1.23  		c.Website = *change.Website
    1.24  	}
    1.25 +	if change.Deleted != nil {
    1.26 +		c.Deleted = *change.Deleted
    1.27 +	}
    1.28  }
    1.29  
    1.30  // ClientChange represents a bundle of options for
    1.31 @@ -112,13 +119,18 @@
    1.32  	Name    *string
    1.33  	Logo    *string
    1.34  	Website *string
    1.35 +	Deleted *bool
    1.36 +}
    1.37 +
    1.38 +func (c ClientChange) Empty() bool {
    1.39 +	return c.Secret == nil && c.OwnerID == nil && c.Name == nil && c.Logo == nil && c.Website == nil && c.Deleted == nil
    1.40  }
    1.41  
    1.42  // Validate checks the ClientChange it is called on
    1.43  // and asserts its internal validity, or lack thereof.
    1.44  func (c ClientChange) Validate() []error {
    1.45  	errors := []error{}
    1.46 -	if c.Secret == nil && c.OwnerID == nil && c.Name == nil && c.Logo == nil && c.Website == nil {
    1.47 +	if c.Empty() {
    1.48  		errors = append(errors, ErrEmptyChange)
    1.49  		return errors
    1.50  	}
    1.51 @@ -255,10 +267,9 @@
    1.52  	getClient(id uuid.ID) (Client, error)
    1.53  	saveClient(client Client) error
    1.54  	updateClient(id uuid.ID, change ClientChange) error
    1.55 -	deleteClient(id uuid.ID) error
    1.56  	listClientsByOwner(ownerID uuid.ID, num, offset int) ([]Client, error)
    1.57  
    1.58 -	addEndpoints(client uuid.ID, endpoint []Endpoint) error
    1.59 +	addEndpoints(endpoint []Endpoint) error
    1.60  	removeEndpoint(client, endpoint uuid.ID) error
    1.61  	getEndpoint(client, endpoint uuid.ID) (Endpoint, error)
    1.62  	checkEndpoint(client uuid.ID, endpoint string) (bool, error)
    1.63 @@ -270,7 +281,7 @@
    1.64  	m.clientLock.RLock()
    1.65  	defer m.clientLock.RUnlock()
    1.66  	c, ok := m.clients[id.String()]
    1.67 -	if !ok {
    1.68 +	if !ok || c.Deleted {
    1.69  		return Client{}, ErrClientNotFound
    1.70  	}
    1.71  	return c, nil
    1.72 @@ -299,27 +310,6 @@
    1.73  	return nil
    1.74  }
    1.75  
    1.76 -func (m *memstore) deleteClient(id uuid.ID) error {
    1.77 -	client, err := m.getClient(id)
    1.78 -	if err != nil {
    1.79 -		return err
    1.80 -	}
    1.81 -	m.clientLock.Lock()
    1.82 -	defer m.clientLock.Unlock()
    1.83 -	delete(m.clients, id.String())
    1.84 -	pos := -1
    1.85 -	for p, item := range m.profileClientLookup[client.OwnerID.String()] {
    1.86 -		if item.Equal(id) {
    1.87 -			pos = p
    1.88 -			break
    1.89 -		}
    1.90 -	}
    1.91 -	if pos >= 0 {
    1.92 -		m.profileClientLookup[client.OwnerID.String()] = append(m.profileClientLookup[client.OwnerID.String()][:pos], m.profileClientLookup[client.OwnerID.String()][pos+1:]...)
    1.93 -	}
    1.94 -	return nil
    1.95 -}
    1.96 -
    1.97  func (m *memstore) listClientsByOwner(ownerID uuid.ID, num, offset int) ([]Client, error) {
    1.98  	ids := m.lookupClientsByProfileID(ownerID.String())
    1.99  	if len(ids) > num+offset {
   1.100 @@ -333,6 +323,9 @@
   1.101  	for _, id := range ids {
   1.102  		client, err := m.getClient(id)
   1.103  		if err != nil {
   1.104 +			if err == ErrClientNotFound {
   1.105 +				continue
   1.106 +			}
   1.107  			return []Client{}, err
   1.108  		}
   1.109  		clients = append(clients, client)
   1.110 @@ -340,10 +333,16 @@
   1.111  	return clients, nil
   1.112  }
   1.113  
   1.114 -func (m *memstore) addEndpoints(client uuid.ID, endpoints []Endpoint) error {
   1.115 +func (m *memstore) addEndpoints(endpoints []Endpoint) error {
   1.116  	m.endpointLock.Lock()
   1.117  	defer m.endpointLock.Unlock()
   1.118 -	m.endpoints[client.String()] = append(m.endpoints[client.String()], endpoints...)
   1.119 +	clients := map[string][]Endpoint{}
   1.120 +	for _, endpoint := range endpoints {
   1.121 +		clients[endpoint.ClientID.String()] = append(clients[endpoint.ClientID.String()], endpoint)
   1.122 +	}
   1.123 +	for client, e := range clients {
   1.124 +		m.endpoints[client] = append(m.endpoints[client], e...)
   1.125 +	}
   1.126  	return nil
   1.127  }
   1.128  
   1.129 @@ -507,7 +506,7 @@
   1.130  		}
   1.131  		endpoints = append(endpoints, endpoint)
   1.132  	}
   1.133 -	err = c.AddEndpoints(client.ID, endpoints)
   1.134 +	err = c.AddEndpoints(endpoints)
   1.135  	if err != nil {
   1.136  		log.Printf("Error adding endpoints: %#+v\n", err)
   1.137  		errors = append(errors, requestError{Slug: requestErrActOfGod})
   1.138 @@ -793,7 +792,9 @@
   1.139  		encode(w, r, http.StatusForbidden, response{Errors: errors})
   1.140  		return
   1.141  	}
   1.142 -	err = c.DeleteClient(id)
   1.143 +	deleted := true
   1.144 +	change := ClientChange{Deleted: &deleted}
   1.145 +	err = c.UpdateClient(id, change)
   1.146  	if err != nil {
   1.147  		if err == ErrClientNotFound {
   1.148  			errors = append(errors, requestError{Slug: requestErrNotFound})
   1.149 @@ -893,7 +894,7 @@
   1.150  		encode(w, r, http.StatusBadRequest, response{Errors: errors})
   1.151  		return
   1.152  	}
   1.153 -	err = c.AddEndpoints(id, endpoints)
   1.154 +	err = c.AddEndpoints(endpoints)
   1.155  	if err != nil {
   1.156  		encode(w, r, http.StatusInternalServerError, actOfGodResponse)
   1.157  		return