From 5e229f7d96244769feb9d93a371f8b96a0846d53 Mon Sep 17 00:00:00 2001 From: Tan Jiang Date: Thu, 16 Mar 2017 16:53:40 +0800 Subject: [PATCH] systeminfo returns flag for ca root --- docs/swagger.yaml | 9 ++++++--- src/ui/api/systeminfo.go | 10 ++++++++-- src/ui/api/systeminfo_test.go | 1 + 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 79d8b2fe5..293894750 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -2025,6 +2025,9 @@ definitions: self_registration: type: boolean description: Indicate whether the Harbor instance enable user to register himself. + has_ca_root: + type: boolean + description: Indicate whether there is a ca root cert file ready for download in the file system. SystemInfo: type: object properties: @@ -2098,7 +2101,7 @@ definitions: type: string description: The host of email server. email_port: - type: string + type: integer description: The port of email server. email_username: type: string @@ -2107,7 +2110,7 @@ definitions: type: string description: The password of email server. email_ssl: - type: string + type: boolean description: Use ssl/tls or not. email_identity: type: string @@ -2162,4 +2165,4 @@ definitions: description: The creation time of repository. update_time: type: string - description: The update time of repository. \ No newline at end of file + description: The update time of repository. diff --git a/src/ui/api/systeminfo.go b/src/ui/api/systeminfo.go index 767d96d7f..5748d0d00 100644 --- a/src/ui/api/systeminfo.go +++ b/src/ui/api/systeminfo.go @@ -44,6 +44,7 @@ type GeneralInfo struct { RegistryURL string `json:"registry_url"` ProjectCreationRestrict string `json:"project_creation_restriction"` SelfRegistration bool `json:"self_registration"` + HasCARoot bool `json:"has_ca_root"` } // validate for validating user if an admin. @@ -88,13 +89,16 @@ func (sia *SystemInfoAPI) GetVolumeInfo() { func (sia *SystemInfoAPI) GetCert() { sia.validate() if sia.isAdmin { - if _, err := os.Stat(defaultRootCert); !os.IsNotExist(err) { + if _, err := os.Stat(defaultRootCert); err == nil { sia.Ctx.Output.Header("Content-Type", "application/octet-stream") sia.Ctx.Output.Header("Content-Disposition", "attachment; filename=ca.crt") http.ServeFile(sia.Ctx.ResponseWriter, sia.Ctx.Request, defaultRootCert) - } else { + } else if os.IsNotExist(err) { log.Error("No certificate found.") sia.CustomAbort(http.StatusNotFound, "No certificate found.") + } else { + log.Errorf("Unexpected error: %v", err) + sia.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) } } sia.CustomAbort(http.StatusForbidden, "") @@ -113,6 +117,7 @@ func (sia *SystemInfoAPI) GetGeneralInfo() { } else { registryURL = l[0] } + _, caStatErr := os.Stat(defaultRootCert) info := GeneralInfo{ AdmiralEndpoint: cfg[comcfg.AdmiralEndpoint].(string), WithAdmiral: config.WithAdmiral(), @@ -121,6 +126,7 @@ func (sia *SystemInfoAPI) GetGeneralInfo() { ProjectCreationRestrict: cfg[comcfg.ProjectCreationRestriction].(string), SelfRegistration: cfg[comcfg.SelfRegistration].(bool), RegistryURL: registryURL, + HasCARoot: caStatErr == nil, } sia.Data["json"] = info sia.ServeJSON() diff --git a/src/ui/api/systeminfo_test.go b/src/ui/api/systeminfo_test.go index f52a94707..80892fd84 100644 --- a/src/ui/api/systeminfo_test.go +++ b/src/ui/api/systeminfo_test.go @@ -48,6 +48,7 @@ func TestGetGeneralInfo(t *testing.T) { err = json.Unmarshal(body, g) assert.Nil(err, fmt.Sprintf("Unexpected Error: %v", err)) assert.Equal(false, g.WithNotary, "with notary should be false") + assert.Equal(true, g.HasCARoot, "has ca root should be true") } func TestGetCert(t *testing.T) {