Skip to content

Commit

Permalink
api: add subscription limits
Browse files Browse the repository at this point in the history
Changes:
- added a new `VALID_SUBSCRIPTION` env variable, it can be `true` or `false` (string type)
- get-defaults API now returns a new `valid_subscription` field
- register API does not accept units without subscription if the controller has a valid subscription
- add API allows maximum 3 units if the controller does not have a valid subscription
  • Loading branch information
gsanchietti committed Apr 11, 2024
1 parent b457186 commit fff4551
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
5 changes: 3 additions & 2 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,9 @@ CGO_ENABLED=0 go build
"fqdn": "controller.ns8.local",
"grafana_path": "/grafana",
"prometheus_path": "/prometheus",
"webssh_path": "/webssh"
"webssh_path": "/webssh",
"valid_subscription": false
},
"message": "success"
}
```
```
8 changes: 8 additions & 0 deletions api/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type Configuration struct {
FQDN string `json:"fqdn"`

CacheTTL string `json:"cache_ttl"`

ValidSubscription string `json:"valid_subscription"`
}

var Config = Configuration{}
Expand Down Expand Up @@ -228,4 +230,10 @@ func Init() {
} else {
Config.CacheTTL = "7200"
}

if os.Getenv("VALID_SUBSCRIPTION") != "" {
Config.ValidSubscription = os.Getenv("VALID_SUBSCRIPTION")
} else {
Config.ValidSubscription = "false"
}
}
9 changes: 5 additions & 4 deletions api/methods/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ func GetDefaults(c *gin.Context) {
Code: 200,
Message: "success",
Data: gin.H{
"fqdn": configuration.Config.FQDN,
"prometheus_path": configuration.Config.PrometheusPath,
"webssh_path": configuration.Config.WebSSHPath,
"grafana_path": configuration.Config.GrafanaPath,
"fqdn": configuration.Config.FQDN,
"prometheus_path": configuration.Config.PrometheusPath,
"webssh_path": configuration.Config.WebSSHPath,
"grafana_path": configuration.Config.GrafanaPath,
"valid_subscription": configuration.Config.ValidSubscription == "true",
},
}))
}
31 changes: 31 additions & 0 deletions api/methods/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,27 @@ func AddUnit(c *gin.Context) {
return
}

// if the controller does not have a subscription, limit the number of units to 3
if configuration.Config.ValidSubscription != "true" {
units, err := ListUnits()
if err != nil {
c.JSON(http.StatusBadRequest, structs.Map(response.StatusBadRequest{
Code: 400,
Message: "can't list units",
Data: err.Error(),
}))
return
}
if len(units) >= 3 {
c.JSON(http.StatusForbidden, structs.Map(response.StatusBadRequest{
Code: 403,
Message: "subscription limit reached",
Data: "",
}))
return
}
}

// check duplicates
if _, err := os.Stat(configuration.Config.OpenVPNCCDDir + "/" + jsonRequest.UnitId); err == nil {
c.JSON(http.StatusConflict, structs.Map(response.StatusConflict{
Expand Down Expand Up @@ -369,6 +390,16 @@ func RegisterUnit(c *gin.Context) {
return
}

// if the controller has a subscription, the unit must have a valid subscription too
if configuration.Config.ValidSubscription == "true" && jsonRequest.SubscriptionType == "" {
c.JSON(http.StatusForbidden, structs.Map(response.StatusBadRequest{
Code: 403,
Message: "subscription is required",
Data: "",
}))
return
}

// check openvpn conf exists
if _, err := os.Stat(configuration.Config.OpenVPNPKIDir + "/issued/" + jsonRequest.UnitId + ".crt"); err == nil {
// read ca
Expand Down

0 comments on commit fff4551

Please sign in to comment.