From bfdbc77a3a18057dade6531dc7b855f812283c28 Mon Sep 17 00:00:00 2001 From: mosen Date: Fri, 7 Oct 2016 12:35:06 +1100 Subject: [PATCH] Non plural packages (#25) * 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 * Rename certificates to certficiate Rename applications to application --- {applications => application}/application.go | 2 +- {applications => application}/datastore.go | 2 +- .../datastore_test.go | 2 +- {applications => application}/statement.go | 2 +- {certificates => certificate}/certificate.go | 2 +- {certificates => certificate}/datastore.go | 2 +- .../datastore_test.go | 2 +- {certificates => certificate}/statement.go | 2 +- .../statement_test.go | 2 +- connect/service.go | 18 +++++++++--------- main.go | 8 ++++---- management/endpoint_certificates.go | 6 +++--- management/endpoint_installedapps.go | 4 ++-- management/service.go | 18 +++++++++--------- 14 files changed, 36 insertions(+), 36 deletions(-) rename {applications => application}/application.go (98%) rename {applications => application}/datastore.go (99%) rename {applications => application}/datastore_test.go (98%) rename {applications => application}/statement.go (99%) rename {certificates => certificate}/certificate.go (93%) rename {certificates => certificate}/datastore.go (99%) rename {certificates => certificate}/datastore_test.go (96%) rename {certificates => certificate}/statement.go (98%) rename {certificates => certificate}/statement_test.go (98%) diff --git a/applications/application.go b/application/application.go similarity index 98% rename from applications/application.go rename to application/application.go index 35c4caeb..ee6b6c7e 100644 --- a/applications/application.go +++ b/application/application.go @@ -1,4 +1,4 @@ -package applications +package application import "database/sql" diff --git a/applications/datastore.go b/application/datastore.go similarity index 99% rename from applications/datastore.go rename to application/datastore.go index 24b65551..697480e2 100644 --- a/applications/datastore.go +++ b/application/datastore.go @@ -1,4 +1,4 @@ -package applications +package application import ( "fmt" diff --git a/applications/datastore_test.go b/application/datastore_test.go similarity index 98% rename from applications/datastore_test.go rename to application/datastore_test.go index e7c18a55..2e00493d 100644 --- a/applications/datastore_test.go +++ b/application/datastore_test.go @@ -1,4 +1,4 @@ -package applications +package application import ( "database/sql" diff --git a/applications/statement.go b/application/statement.go similarity index 99% rename from applications/statement.go rename to application/statement.go index d0d8e631..3b32efba 100644 --- a/applications/statement.go +++ b/application/statement.go @@ -1,4 +1,4 @@ -package applications +package application import ( "fmt" diff --git a/certificates/certificate.go b/certificate/certificate.go similarity index 93% rename from certificates/certificate.go rename to certificate/certificate.go index 9468d9e1..b75d2825 100644 --- a/certificates/certificate.go +++ b/certificate/certificate.go @@ -1,4 +1,4 @@ -package certificates +package certificate type Certificate struct { UUID string `db:"certificate_uuid" json:"uuid"` diff --git a/certificates/datastore.go b/certificate/datastore.go similarity index 99% rename from certificates/datastore.go rename to certificate/datastore.go index c96395fa..48fa5fd9 100644 --- a/certificates/datastore.go +++ b/certificate/datastore.go @@ -1,4 +1,4 @@ -package certificates +package certificate import ( "fmt" diff --git a/certificates/datastore_test.go b/certificate/datastore_test.go similarity index 96% rename from certificates/datastore_test.go rename to certificate/datastore_test.go index 04f6427d..d2f3a85d 100644 --- a/certificates/datastore_test.go +++ b/certificate/datastore_test.go @@ -1,4 +1,4 @@ -package certificates +package certificate import ( "database/sql" diff --git a/certificates/statement.go b/certificate/statement.go similarity index 98% rename from certificates/statement.go rename to certificate/statement.go index fd16b528..a2432913 100644 --- a/certificates/statement.go +++ b/certificate/statement.go @@ -1,4 +1,4 @@ -package certificates +package certificate import ( "fmt" diff --git a/certificates/statement_test.go b/certificate/statement_test.go similarity index 98% rename from certificates/statement_test.go rename to certificate/statement_test.go index bbdd4eb4..c1085328 100644 --- a/certificates/statement_test.go +++ b/certificate/statement_test.go @@ -1,4 +1,4 @@ -package certificates +package certificate import ( "testing" diff --git a/connect/service.go b/connect/service.go index df334f1b..32f00e96 100644 --- a/connect/service.go +++ b/connect/service.go @@ -5,8 +5,8 @@ import ( "encoding/json" "fmt" "github.com/micromdm/mdm" - apps "github.com/micromdm/micromdm/applications" - "github.com/micromdm/micromdm/certificates" + "github.com/micromdm/micromdm/application" + "github.com/micromdm/micromdm/certificate" "github.com/micromdm/micromdm/command" "github.com/micromdm/micromdm/device" "github.com/pkg/errors" @@ -22,7 +22,7 @@ type Service interface { } // NewService creates a mdm service -func NewService(devices device.Datastore, apps apps.Datastore, certs certificates.Datastore, cs command.Service) Service { +func NewService(devices device.Datastore, apps application.Datastore, certs certificate.Datastore, cs command.Service) Service { return &service{ commands: cs, devices: devices, @@ -33,9 +33,9 @@ func NewService(devices device.Datastore, apps apps.Datastore, certs certificate type service struct { devices device.Datastore - apps apps.Datastore + apps application.Datastore commands command.Service - certs certificates.Datastore + certs certificate.Datastore } // Acknowledge a response from a device. @@ -157,7 +157,7 @@ func (svc service) ackInstalledApplicationList(req mdm.Response) error { return fmt.Errorf("clearing applications for device: %s", err) } - var requestApps []apps.DeviceApplication = make([]apps.DeviceApplication, len(req.InstalledApplicationList)) + var requestApps []application.DeviceApplication = make([]application.DeviceApplication, len(req.InstalledApplicationList)) // Update or insert application records that do not exist, returning the UUID so that it can be inserted for // the device sending the response. for i, reqApp := range req.InstalledApplicationList { @@ -171,7 +171,7 @@ func (svc service) ackInstalledApplicationList(req mdm.Response) error { dynamicSize := sql.NullInt64{} dynamicSize.Scan(reqApp.DynamicSize) - newApp := apps.DeviceApplication{ + newApp := application.DeviceApplication{ DeviceUUID: dev.UUID, Name: reqApp.Name, Identifier: identifier, @@ -203,9 +203,9 @@ func (svc service) ackCertificateList(req mdm.Response) error { return errors.Wrap(err, "getting a device record by udid") } - var certs []certificates.Certificate = []certificates.Certificate{} + var certs []certificate.Certificate = []certificate.Certificate{} for _, cert := range req.CertificateList { - newCert := certificates.Certificate{ + newCert := certificate.Certificate{ CommonName: cert.CommonName, IsIdentity: cert.IsIdentity, Data: cert.Data, diff --git a/main.go b/main.go index 1c432907..5f8ccced 100644 --- a/main.go +++ b/main.go @@ -14,8 +14,8 @@ import ( "github.com/RobotsAndPencils/buford/push" "github.com/go-kit/kit/log" "github.com/micromdm/dep" - "github.com/micromdm/micromdm/applications" - "github.com/micromdm/micromdm/certificates" + "github.com/micromdm/micromdm/application" + mdmCert "github.com/micromdm/micromdm/certificate" "github.com/micromdm/micromdm/checkin" "github.com/micromdm/micromdm/command" "github.com/micromdm/micromdm/connect" @@ -189,7 +189,7 @@ func main() { os.Exit(1) } - appsDB, err := applications.NewDB( + appsDB, err := application.NewDB( "postgres", *flPGconn, logger, @@ -199,7 +199,7 @@ func main() { os.Exit(1) } - certsDB, err := certificates.NewDB( + certsDB, err := mdmCert.NewDB( "postgres", *flPGconn, logger, diff --git a/management/endpoint_certificates.go b/management/endpoint_certificates.go index b7bcc90f..3f0ce23b 100644 --- a/management/endpoint_certificates.go +++ b/management/endpoint_certificates.go @@ -3,7 +3,7 @@ package management import ( "encoding/json" "github.com/go-kit/kit/endpoint" - "github.com/micromdm/micromdm/certificates" + "github.com/micromdm/micromdm/certificate" "golang.org/x/net/context" "net/http" ) @@ -13,8 +13,8 @@ type listCertificatesRequest struct { } type listCertificatesResponse struct { - certificates []certificates.Certificate `json:"certificates,omitempty"` - Err error `json:"error,omitempty"` + certificates []certificate.Certificate `json:"certificates,omitempty"` + Err error `json:"error,omitempty"` } func (r listCertificatesResponse) error() error { return r.Err } diff --git a/management/endpoint_installedapps.go b/management/endpoint_installedapps.go index e74a9029..61bbb42e 100644 --- a/management/endpoint_installedapps.go +++ b/management/endpoint_installedapps.go @@ -2,7 +2,7 @@ package management import ( "github.com/go-kit/kit/endpoint" - "github.com/micromdm/micromdm/applications" + "github.com/micromdm/micromdm/application" "golang.org/x/net/context" ) @@ -11,7 +11,7 @@ type installedAppsRequest struct { } type installedAppsResponse struct { - applications []applications.Application + applications []application.Application Err error `json:"error,omitempty"` } diff --git a/management/service.go b/management/service.go index cb256578..b084f282 100644 --- a/management/service.go +++ b/management/service.go @@ -5,8 +5,8 @@ import ( "github.com/RobotsAndPencils/buford/payload" "github.com/RobotsAndPencils/buford/push" "github.com/micromdm/dep" - "github.com/micromdm/micromdm/applications" - "github.com/micromdm/micromdm/certificates" + "github.com/micromdm/micromdm/application" + "github.com/micromdm/micromdm/certificate" "github.com/micromdm/micromdm/device" "github.com/micromdm/micromdm/workflow" "github.com/pkg/errors" @@ -31,10 +31,10 @@ type Service interface { Device(uuid string) (*device.Device, error) // Installed Applications - InstalledApps(deviceUUID string) ([]applications.Application, error) + InstalledApps(deviceUUID string) ([]application.Application, error) // Installed Certificates - Certificates(deviceUUID string) ([]certificates.Certificate, error) + Certificates(deviceUUID string) ([]certificate.Certificate, error) // AssignWorkflow assigns a workflow to a device AssignWorkflow(deviceUUID, workflowUUID string) error @@ -48,7 +48,7 @@ type Service interface { } // NewService creates a management service -func NewService(ds device.Datastore, ws workflow.Datastore, dc dep.Client, ps *push.Service, as applications.Datastore, cs certificates.Datastore) Service { +func NewService(ds device.Datastore, ws workflow.Datastore, dc dep.Client, ps *push.Service, as application.Datastore, cs certificate.Datastore) Service { return &service{ devices: ds, depClient: dc, @@ -64,8 +64,8 @@ type service struct { devices device.Datastore workflows workflow.Datastore pushsvc *push.Service - applications applications.Datastore - certificates certificates.Datastore + applications application.Datastore + certificates certificate.Datastore } func (svc service) Push(deviceUDID string) (string, error) { @@ -172,7 +172,7 @@ func (svc service) AssignWorkflow(deviceUUID, workflowUUID string) error { return svc.devices.Save("assignWorkflow", dev) } -func (svc service) InstalledApps(deviceUUID string) ([]applications.Application, error) { +func (svc service) InstalledApps(deviceUUID string) ([]application.Application, error) { apps, err := svc.applications.GetApplicationsByDeviceUUID(deviceUUID) if err != nil { return nil, errors.Wrap(err, "management: installed apps") @@ -181,7 +181,7 @@ func (svc service) InstalledApps(deviceUUID string) ([]applications.Application, return apps, nil } -func (svc service) Certificates(deviceUUID string) ([]certificates.Certificate, error) { +func (svc service) Certificates(deviceUUID string) ([]certificate.Certificate, error) { certs, err := svc.certificates.GetCertificatesByDeviceUUID(deviceUUID) if err != nil { return nil, errors.Wrap(err, "management: certificates")