harness-drone/service/license/load.go
2020-10-20 15:30:22 -04:00

81 lines
2 KiB
Go

// Copyright 2019 Drone IO, Inc.
//
// 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.
// +build !nolimit
// +build !oss
package license
import (
"strings"
"github.com/drone/drone/core"
"github.com/drone/go-license/license"
"github.com/drone/go-license/license/licenseutil"
)
// embedded public key used to verify license signatures.
var publicKey = []byte("GB/hFnXEg63vDZ2W6mKFhLxZTuxMrlN/C/0iVZ2LfPQ=")
// License renewal endpoint.
const licenseEndpoint = "https://license.drone.io/api/v1/license/renew"
// Trial returns a default license with trial terms based
// on the source code management system.
func Trial(provider string) *core.License {
switch provider {
case "gitea", "gogs":
return &core.License{
Kind: core.LicenseTrial,
Repos: 0,
Users: 0,
Builds: 0,
Nodes: 0,
}
default:
return &core.License{
Kind: core.LicenseTrial,
Repos: 0,
Users: 0,
Builds: 5000,
Nodes: 0,
}
}
}
// Load loads the license from file.
func Load(path string) (*core.License, error) {
pub, err := licenseutil.DecodePublicKey(publicKey)
if err != nil {
return nil, err
}
var decoded *license.License
if strings.HasPrefix(path, "-----BEGIN LICENSE KEY-----") {
decoded, err = license.Decode([]byte(path), pub)
} else {
decoded, err = license.DecodeFile(path, pub)
}
if err != nil {
return nil, err
}
license := new(core.License)
license.Expires = decoded.Exp
license.Licensor = decoded.Cus
license.Subscription = decoded.Sub
license.Users = int64(decoded.Lim)
return license, err
}