Skip to content
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

Check errors #431

Merged
merged 17 commits into from
Dec 13, 2016
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ install:
- go get -u golang.org/x/net/context
- go get -u github.com/HewlettPackard/gas
- go get -u gopkg.in/godo.v2/cmd/godo
- go get -u github.com/kisielk/errcheck
- export GO15VENDOREXPERIMENT=1
- glide install

Expand All @@ -30,3 +31,4 @@ script:
- go vet ./management/...
- test -z "$(golint ./Gododir/... | tee /dev/stderr)"
- go vet ./Gododir/...
- set -e; for v in ./arm/... ./datalake-store/... ./Gododir/... ./management/... ./storage/...; do (errcheck -ignoretests "$v" | grep -v 'Close()' | tee /dev/stderr); done
14 changes: 10 additions & 4 deletions management/examples/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,19 @@ func main() {

fmt.Println("Create virtual machine")
role := vmutils.NewVMConfiguration(dnsName, vmSize)
vmutils.ConfigureDeploymentFromPlatformImage(
if err := vmutils.ConfigureDeploymentFromPlatformImage(
&role,
vmImage,
fmt.Sprintf("http://%s.blob.core.windows.net/%s/%s.vhd", storageAccount, dnsName, dnsName),
"")
vmutils.ConfigureForLinux(&role, dnsName, userName, userPassword)
vmutils.ConfigureWithPublicSSH(&role)
""); err != nil {
panic(err)
}
if err := vmutils.ConfigureForLinux(&role, dnsName, userName, userPassword); err != nil {
panic(err)
}
if err := vmutils.ConfigureWithPublicSSH(&role); err != nil {
panic(err)
}

fmt.Println("Deploy")
operationID, err := virtualmachine.NewClient(client).
Expand Down
4 changes: 3 additions & 1 deletion management/vmutils/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ func AddAzureDockerVMExtensionConfiguration(role *vm.Role, dockerPort int, versi
return fmt.Errorf(errParamNotSpecified, "role")
}

ConfigureWithExternalPort(role, "docker", dockerPort, dockerPort, vm.InputEndpointProtocolTCP)
if err := ConfigureWithExternalPort(role, "docker", dockerPort, dockerPort, vm.InputEndpointProtocolTCP); err != nil {
return err
}

publicConfiguration, err := createDockerPublicConfig(dockerPort)
if err != nil {
Expand Down
13 changes: 6 additions & 7 deletions storage/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,7 @@ func (b BlobStorageClient) SetContainerPermissions(container string, containerPe
}

if resp != nil {
defer func() {
err = resp.body.Close()
}()
defer resp.body.Close()

if resp.statusCode != http.StatusOK {
return errors.New("Unable to set permissions")
Expand Down Expand Up @@ -575,9 +573,7 @@ func (b BlobStorageClient) GetContainerPermissions(container string, timeout int
// containerAccess. Blob, Container, empty
containerAccess := resp.headers.Get(http.CanonicalHeaderKey(ContainerAccessHeader))

defer func() {
err = resp.body.Close()
}()
defer resp.body.Close()

var out AccessPolicy
err = xmlUnmarshal(resp.body, &out.SignedIdentifiersList)
Expand Down Expand Up @@ -1479,7 +1475,10 @@ func (b BlobStorageClient) GetBlobSASURIWithSignedIPAndProtocol(container, name
return "", err
}

sig := b.client.computeHmac256(stringToSign)
sig, err := b.client.computeHmac256(stringToSign)
if err != nil {
return "", err
}
sasParams := url.Values{
"sv": {b.client.apiVersion},
"se": {signedExpiry},
Expand Down
16 changes: 11 additions & 5 deletions storage/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,12 @@ func (c Client) GetFileService() FileServiceClient {
return FileServiceClient{c}
}

func (c Client) createAuthorizationHeader(canonicalizedString string) string {
signature := c.computeHmac256(canonicalizedString)
return fmt.Sprintf("%s %s:%s", "SharedKey", c.getCanonicalizedAccountName(), signature)
func (c Client) createAuthorizationHeader(canonicalizedString string) (string, error) {
signature, err := c.computeHmac256(canonicalizedString)
if err != nil {
return "", err
}
return fmt.Sprintf("%s %s:%s", "SharedKey", c.getCanonicalizedAccountName(), signature), nil
}

func (c Client) getAuthorizationHeader(verb, url string, headers map[string]string) (string, error) {
Expand All @@ -246,7 +249,7 @@ func (c Client) getAuthorizationHeader(verb, url string, headers map[string]stri
}

canonicalizedString := c.buildCanonicalizedString(verb, headers, canonicalizedResource)
return c.createAuthorizationHeader(canonicalizedString), nil
return c.createAuthorizationHeader(canonicalizedString)
}

func (c Client) getStandardHeaders() map[string]string {
Expand Down Expand Up @@ -500,7 +503,10 @@ func (c Client) createSharedKeyLite(url string, headers map[string]string) (stri
}
strToSign := headers["x-ms-date"] + "\n" + can

hmac := c.computeHmac256(strToSign)
hmac, err := c.computeHmac256(strToSign)
if err != nil {
return "", err
}
return fmt.Sprintf("SharedKeyLite %s:%s", c.accountName, hmac), nil
}

Expand Down
4 changes: 3 additions & 1 deletion storage/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,7 @@ func (s *StorageClientSuite) Test_createAuthorizationHeader(c *chk.C) {

canonicalizedString := `foobarzoo`
expected := `SharedKey foo:h5U0ATVX6SpbFX1H6GNuxIMeXXCILLoIvhflPtuQZ30=`
c.Assert(cli.createAuthorizationHeader(canonicalizedString), chk.Equals, expected)
ah, err := cli.createAuthorizationHeader(canonicalizedString)
c.Assert(err, chk.IsNil)
c.Assert(ah, chk.Equals, expected)
}
7 changes: 2 additions & 5 deletions storage/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ func (f FileServiceClient) ListDirsAndFiles(path string, params ListDirsAndFiles
if err != nil {
return out, err
}

defer resp.body.Close()
err = xmlUnmarshal(resp.body, &out)
return out, err
Expand Down Expand Up @@ -302,7 +301,6 @@ func (f FileServiceClient) ListShares(params ListSharesParameters) (ShareListRes
if err != nil {
return out, err
}

defer resp.body.Close()
err = xmlUnmarshal(resp.body, &out)
return out, err
Expand All @@ -323,7 +321,7 @@ func (f FileServiceClient) listContent(path string, params url.Values, extraHead
}

if err = checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil {
resp.body.Close()
_ = resp.body.Close()
return nil, err
}

Expand Down Expand Up @@ -400,7 +398,6 @@ func (f FileServiceClient) modifyRange(path string, bytes io.Reader, fileRange F
if err != nil {
return err
}

defer resp.body.Close()
return checkRespCode(resp.statusCode, []int{http.StatusCreated})
}
Expand All @@ -423,7 +420,7 @@ func (f FileServiceClient) GetFile(path string, fileRange *FileRange) (*FileStre
}

if err = checkRespCode(resp.statusCode, []int{http.StatusOK, http.StatusPartialContent}); err != nil {
resp.body.Close()
defer resp.body.Close()
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion storage/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (c QueueServiceClient) DeleteMessage(queue, messageID, popReceipt string) e
if err != nil {
return err
}
defer resp.body.Close()
deferresp.body.Close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clumsy me

return checkRespCode(resp.statusCode, []int{http.StatusNoContent})
}

Expand Down
4 changes: 3 additions & 1 deletion storage/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func (c *TableServiceClient) QueryTables() ([]AzureTable, error) {
}

buf := new(bytes.Buffer)
buf.ReadFrom(resp.body)
if _, err := buf.ReadFrom(resp.body); err != nil {
return nil, err
}

var respArray queryTablesResponse
if err := json.Unmarshal(buf.Bytes(), &respArray); err != nil {
Expand Down
13 changes: 7 additions & 6 deletions storage/table_entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,7 @@ func (c *TableServiceClient) execTable(table AzureTable, entity TableEntity, spe

headers["Content-Length"] = fmt.Sprintf("%d", buf.Len())

var err error
var resp *odataResponse

resp, err = c.client.execTable(method, uri, headers, &buf)
resp, err := c.client.execTable(method, uri, headers, &buf)

if err != nil {
return 0, err
Expand Down Expand Up @@ -327,8 +324,12 @@ func deserializeEntity(retType reflect.Type, reader io.Reader) ([]TableEntity, e
}

// Reset PartitionKey and RowKey
tEntries[i].SetPartitionKey(pKey)
tEntries[i].SetRowKey(rKey)
if err := tEntries[i].SetPartitionKey(pKey); err != nil {
return nil, err
}
if err := tEntries[i].SetRowKey(rKey); err != nil {
return nil, err
}
}

return tEntries, nil
Expand Down
8 changes: 5 additions & 3 deletions storage/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import (
"time"
)

func (c Client) computeHmac256(message string) string {
func (c Client) computeHmac256(message string) (string, error) {
h := hmac.New(sha256.New, c.accountKey)
h.Write([]byte(message))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
if _, err := h.Write([]byte(message)); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda mixed feelings about changing this. the hmac.inner == sha256 type's Write() will never return an error as everything is in-memory. 😕

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this change causes many changes in the upstream callers whereas the error can be just discarded. wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errcheck doesn't like it. In the other hand, gas doesn't really care. Looking at all this again, I actually think gas is more reasonable. I think it would be better if instead of gas -skip=*/arm/*/models.go -skip=*/management/examples/*.go -skip=*vendor* -skip=*/Gododir/* ./..., I rewrote that line so for all this skipped files I just exclude whatever rule they break. For example, I'm skipping all the models.go files because they "break" rule G101.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And we can then get rid of errcheck :D

return "", fmt.Errorf("Failed computing to hmac256 : %v", err)
}
return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
}

func currentTimeRfc1123Formatted() string {
Expand Down