harness-drone/service/license/load.go

90 lines
2.1 KiB
Go
Raw Permalink Normal View History

// 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.
2019-02-19 23:56:41 +00:00
// +build !nolimit
2020-08-25 20:07:13 +00:00
// +build !oss
2019-02-19 23:56:41 +00:00
package license
import (
2021-07-02 19:39:34 +00:00
"encoding/json"
2019-11-05 21:11:53 +00:00
"strings"
2019-02-19 23:56:41 +00:00
"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,
2019-02-19 23:56:41 +00:00
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
}
2019-11-05 21:11:53 +00:00
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)
}
2019-02-19 23:56:41 +00:00
if err != nil {
return nil, err
}
license := new(core.License)
license.Expires = decoded.Exp
2019-03-13 21:47:47 +00:00
license.Licensor = decoded.Cus
license.Subscription = decoded.Sub
2020-10-20 19:30:22 +00:00
license.Users = int64(decoded.Lim)
2021-07-02 19:39:34 +00:00
if decoded.Dat != nil {
dat := new(core.License)
json.Unmarshal(decoded.Dat, dat)
license.Repos = dat.Repos
}
2019-02-19 23:56:41 +00:00
return license, err
}