harness-drone/store/secret/scan.go
2019-02-20 15:15:29 -08:00

81 lines
2.2 KiB
Go

// Copyright 2019 Drone IO, Inc.
// Copyright 2016 The Linux Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package secret
import (
"database/sql"
"github.com/drone/drone/core"
"github.com/drone/drone/store/shared/db"
"github.com/drone/drone/store/shared/encrypt"
)
// helper function converts the User structure to a set
// of named query parameters.
func toParams(encrypt encrypt.Encrypter, secret *core.Secret) (map[string]interface{}, error) {
ciphertext, err := encrypt.Encrypt(secret.Data)
if err != nil {
return nil, err
}
return map[string]interface{}{
"secret_id": secret.ID,
"secret_repo_id": secret.RepoID,
"secret_name": secret.Name,
"secret_data": ciphertext,
"secret_pull_request": secret.PullRequest,
"secret_pull_request_push": secret.PullRequestPush,
}, nil
}
// helper function scans the sql.Row and copies the column
// values to the destination object.
func scanRow(encrypt encrypt.Encrypter, scanner db.Scanner, dst *core.Secret) error {
var ciphertext []byte
err := scanner.Scan(
&dst.ID,
&dst.RepoID,
&dst.Name,
&ciphertext,
&dst.PullRequest,
&dst.PullRequestPush,
)
if err != nil {
return err
}
plaintext, err := encrypt.Decrypt(ciphertext)
if err != nil {
return err
}
dst.Data = plaintext
return nil
}
// helper function scans the sql.Row and copies the column
// values to the destination object.
func scanRows(encrypt encrypt.Encrypter, rows *sql.Rows) ([]*core.Secret, error) {
defer rows.Close()
secrets := []*core.Secret{}
for rows.Next() {
sec := new(core.Secret)
err := scanRow(encrypt, rows, sec)
if err != nil {
return nil, err
}
secrets = append(secrets, sec)
}
return secrets, nil
}