Skip to content

Commit d9c09f9

Browse files
author
Julien Kassar
committed
Add volumedriver resource type
1 parent bab16bb commit d9c09f9

File tree

5 files changed

+111
-2
lines changed

5 files changed

+111
-2
lines changed

docker/allow/container.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ func ContainerCreate(req authorization.Request, config *types.Config) *types.All
8989
}
9090
}
9191

92+
if len(cc.HostConfig.VolumeDriver) > 0 {
93+
if !p.Validate(config.Username, "volumedriver", cc.HostConfig.VolumeDriver, "") {
94+
return &types.AllowResult{Allow: false, Msg: fmt.Sprintf("Volume driver %s is not allowed", cc.HostConfig.VolumeDriver)}
95+
}
96+
}
97+
9298
if len(cc.HostConfig.CapAdd) > 0 {
9399
for _, c := range cc.HostConfig.CapAdd {
94100
if !p.Validate(config.Username, "capability", c, "") {

docker/allow/volume.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package allow
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/docker/docker/api/types/volume"
7+
"github.com/docker/go-plugins-helpers/authorization"
8+
"github.com/juliengk/go-log"
9+
"github.com/juliengk/go-log/driver"
10+
"github.com/juliengk/go-utils"
11+
"github.com/juliengk/go-utils/json"
12+
"github.com/kassisol/hbm/docker/allow/types"
13+
policyobj "github.com/kassisol/hbm/object/policy"
14+
"github.com/kassisol/hbm/version"
15+
)
16+
17+
func VolumeCreate(req authorization.Request, config *types.Config) *types.AllowResult {
18+
vol := &volume.VolumesCreateBody{}
19+
20+
err := json.Decode(req.RequestBody, vol)
21+
if err != nil {
22+
return &types.AllowResult{Allow: false, Error: err.Error()}
23+
}
24+
25+
defer utils.RecoverFunc()
26+
27+
l, _ := log.NewDriver("standard", nil)
28+
29+
p, err := policyobj.New("sqlite", config.AppPath)
30+
if err != nil {
31+
l.WithFields(driver.Fields{
32+
"storagedriver": "sqlite",
33+
"logdriver": "standard",
34+
"version": version.Version,
35+
}).Fatal(err)
36+
}
37+
defer p.End()
38+
39+
if len(vol.Driver) > 0 {
40+
if !p.Validate(config.Username, "volumedriver", vol.Driver, "") {
41+
return &types.AllowResult{Allow: false, Msg: fmt.Sprintf("Volume driver %s is not allowed", vol.Driver)}
42+
}
43+
}
44+
45+
if len(vol.DriverOpts) > 0 {
46+
for k, v := range vol.DriverOpts {
47+
if vol.Driver == "local" && k == "type" && v == "tmpfs" {
48+
if !p.Validate(config.Username, "config", "container_create_param_tmpfs", "") {
49+
return &types.AllowResult{Allow: false, Msg: "--tmpfs param is not allowed"}
50+
}
51+
}
52+
}
53+
}
54+
55+
return &types.AllowResult{Allow: true}
56+
}

docker/endpoint/endpoint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func GetUris() *uri.URIs {
5858
uris.Register("POST", `^/networks/prune`, allow.True, "network_prune", "network prune", "Delete unused networks")
5959

6060
uris.Register("GET", `^/volumes$`, allow.True, "volume_list", "volume ls", "List volumes")
61-
uris.Register("POST", `^/volumes/create`, allow.True, "volume_create", "volume create", "Create a volume")
61+
uris.Register("POST", `^/volumes/create`, allow.VolumeCreate, "volume_create", "volume create", "Create a volume")
6262
uris.Register("GET", `^/volumes/(.+)`, allow.True, "volume_inspect", "volume inspect", "Inspect a volume")
6363
uris.Register("DELETE", `^/volumes/(.+)`, allow.True, "volume_remove", "volume rm", "Instruct the driver to remove the volume")
6464
uris.Register("POST", `^/volumes/prune`, allow.True, "volume_prune", "volume prune", "Delete unused volumes")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package volumedriver
2+
3+
import (
4+
"github.com/kassisol/hbm/docker/resource"
5+
"github.com/kassisol/hbm/docker/resource/driver"
6+
)
7+
8+
type Config struct{}
9+
10+
func init() {
11+
resource.RegisterDriver("volumedriver", New)
12+
}
13+
14+
func New() (driver.Resourcer, error) {
15+
return &Config{}, nil
16+
}
17+
18+
func (c *Config) List() interface{} {
19+
return []string{}
20+
}
21+
22+
func (c *Config) Valid(value string) error {
23+
return nil
24+
}
25+
26+
func (c *Config) ValidOptions(options map[string]string) error {
27+
return nil
28+
}

docs/reference/commandline/resource_add.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Usage:
1818

1919
Flags:
2020
-o, --option value Specify options (default [])
21-
-t, --type string Set resource type (action|capability|config|device|dns|image|logdriver|logopt|plugin|port|registry|volume) (default "action")
21+
-t, --type string Set resource type (action|capability|config|device|dns|image|logdriver|logopt|plugin|port|registry|volume|volumedriver) (default "action")
2222
-v, --value string Set resource value
2323
```
2424

@@ -359,6 +359,25 @@ NAME TYPE VALUE OPTION
359359
resource1 volume /path/to/dir1
360360
```
361361

362+
---
363+
### Volume Driver
364+
#### Type
365+
`volumedriver`
366+
367+
#### Value
368+
Any volume driver
369+
370+
#### Option
371+
372+
#### Examples
373+
374+
```bash
375+
# hbm resource add --type volumedriver --value kassisol/gitvol resource1
376+
# hbm resource ls -f "type=volumedriver"
377+
NAME TYPE VALUE OPTION COLLECTIONS
378+
resource1 volumedriver kassisol/gitvol
379+
```
380+
362381
## Related information
363382

364383
* [resource_find](resource_find.md)

0 commit comments

Comments
 (0)