Certificates statements (#24)

* Stub new method for application datastore
Handle command failures more gracefully than continuously retrying. Failures are not logged
Add table for certificates.

(cherry picked from commit 9f580a6)

* Remove methods with signature NewDatastore() from datastores and injection of the logger instance into each datastore because groob is a doodoo head :)
Move NewDatastore methods into unit tests
Reinstate NewDB test
Create certificates package including a Certificate type and a datastore.
Began working on a method ackCertificateList in the connect service.
Boilerplate for management service test.
Renamed some tables in certificates migration, hopefully nobody notices.
Applications response handler should be omitted from this branch.

(cherry picked from commit 8bc2fc7)

* Add device uuid to certificate, as there will never be a normalised form of the certificate data.
Change statments to reflect table change from certificates to devices_certificates
Add the CertificateList request type to the connect service's Acknowledge method
Add certificates datastore to the management service, and add endpoints and request/responses for retrieving certificates by device uuid to the management endpoujnt.
Kludge. commits regarding app service will be amended.
(cherry picked from commit c723c80)

* Don't forget the endpoint definition for certificates!

(cherry picked from commit 4e5fc1f)

* Use byte field for certificate data.

(cherry picked from commit 66d5036)

* Fix empty import in certificates

(cherry picked from commit b6d4dca)

* Add table for certificates.

(cherry picked from commit 06bc231)

* Renamed some tables in certificates migration, hopefully nobody notices.
Rebase develop onto master

(cherry picked from commit 775484d)

* Certificate list responses are saved via replacing the entire certificate list on a per device basis. This is because neither the common name nor the data could be used as a unique constraint in the certificates table.
Few small changes to imports/style.

(cherry picked from commit e0a63e1)

* Properly rollback if certificate insert fails for any certificate in a response.

(cherry picked from commit 99e1ea1)

* Fix several incorrect statements and struct tags which were preventing the certificates management endpoint from listing certs.
Certificate listing is working without a base64 encoded representation of each certificate.

(cherry picked from commit 685dfef)

* Add commands index to command datastore.
Add simple test for commands index
Add commands index endpoint

(cherry picked from commit 4e2a2a2)

* Add handler for GET /mdm/commands

(cherry picked from commit ae71b8f)

* Added Find() method to commands datastore so that the request that matches a response can be retrieved by the connect service.
Added Find() method to command service

(cherry picked from commit ebceb28)

* Fix globally scoped vars in command datastore test suite

* groob prefers inline definition of struct members.

* Uppercase CommandUuid

* Add ackCertificateList to connect service, accidentally omitted from installed_certificate_list PR #14
Make push endpoint conform to Errorer interface.
Wrap error return in management service.
Add missing certsDB in main.go, should have been part of installed_certificate_list PR #14

* Fix erraneous comma in mdmEnrollResponse

* Include certificate statement builders.
These are necessary to support the current revision of the installed_certificates package.
This commit is contained in:
mosen
2016-10-07 12:26:05 +11:00
committed by GitHub
parent aa0a31bec2
commit 66dc2beb7f
2 changed files with 148 additions and 0 deletions

80
certificates/statement.go Normal file
View File

@@ -0,0 +1,80 @@
package certificates
import (
"fmt"
"strconv"
"strings"
)
// Where struct wraps a where clause.
// Basic usage is Where{"field","value"} for WHERE field = value
// The Operator field can be used if you need < > = != LIKE / NOT LIKE operators
type Where struct {
Field string
Value interface{}
Operator string
}
// Stringer produces the WHERE condition
func (w Where) String() string {
var operator string = w.Operator
if w.Operator == "" {
operator = "="
}
var quotedValue string
switch w.Value.(type) {
case string:
quotedValue = fmt.Sprintf("'%s'", w.Value)
case nil:
operator = "IS"
quotedValue = "NULL"
case bool:
if w.Value.(bool) == true {
quotedValue = "true"
} else {
quotedValue = "false"
}
case []string: // IN("strings...")
operator = "IN"
inValues := w.Value.([]string)
quotedValue = "('" + strings.Join(inValues, "','") + "')"
case int:
quotedValue = strconv.Itoa(w.Value.(int))
}
return fmt.Sprintf("%s %s %s", w.Field, operator, quotedValue)
}
// whereer is for building args passed into a method which finds resources
type whereer interface {
where() string
}
type WhereAnd []Where
func (wa WhereAnd) String() string {
var result string
for i, cond := range wa {
result += cond.String()
if i < len(wa)-1 {
result += " AND "
}
}
return result
}
type WhereOr []Where
func (wo WhereOr) String() string {
var result string
for i, cond := range wo {
result += cond.String()
if i < len(wo)-1 {
result += " OR "
}
}
return result
}

View File

@@ -0,0 +1,68 @@
package certificates
import (
"testing"
)
var whereTests = []struct {
when Where
then string
}{
{Where{"field", "value", "="}, "field = 'value'"},
{Where{"field", 1, "="}, "field = 1"},
{Where{"field", false, "="}, "field = false"},
{Where{"field", []string{"foo", "bar"}, "IN"}, "field IN ('foo','bar')"},
{Where{"field", "%foo%", "LIKE"}, "field LIKE '%foo%'"},
{Where{"field", "bar", "!="}, "field != 'bar'"},
{Where{"field", nil, "IS"}, "field IS NULL"},
}
func TestWhere_String(t *testing.T) {
for _, test := range whereTests {
v := test.when.String()
if v != test.then {
t.Error(
"Expected", test.then,
"got", v,
)
}
}
}
var waTests = []struct {
when WhereAnd
then string
}{
{WhereAnd{Where{"field", "value", "="}, Where{"field", "bar", "="}}, "field = 'value' AND field = 'bar'"},
}
func TestWhereAnd_String(t *testing.T) {
for _, test := range waTests {
v := test.when.String()
if v != test.then {
t.Error(
"Expected", test.then,
"got", v,
)
}
}
}
var woTests = []struct {
when WhereOr
then string
}{
{WhereOr{Where{"field", "value", "="}, Where{"field", "bar", "="}}, "field = 'value' OR field = 'bar'"},
}
func TestWhereOr_String(t *testing.T) {
for _, test := range woTests {
v := test.when.String()
if v != test.then {
t.Error(
"Expected", test.then,
"got", v,
)
}
}
}