Skip to content

Commit 514ca09

Browse files
committedNov 19, 2016
api: types: keep info.SecurityOptions a string slice
Signed-off-by: Antonio Murdaca <[email protected]>
1 parent 96f50e9 commit 514ca09

File tree

8 files changed

+84
-57
lines changed

8 files changed

+84
-57
lines changed
 

‎api/server/router/system/system_routes.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/docker/docker/api/types/registry"
1717
timetypes "github.com/docker/docker/api/types/time"
1818
"github.com/docker/docker/api/types/versions"
19-
"github.com/docker/docker/api/types/versions/v1p24"
2019
"github.com/docker/docker/pkg/ioutils"
2120
"golang.org/x/net/context"
2221
)
@@ -42,16 +41,24 @@ func (s *systemRouter) getInfo(ctx context.Context, w http.ResponseWriter, r *ht
4241

4342
if versions.LessThan(httputils.VersionFromContext(ctx), "1.25") {
4443
// TODO: handle this conversion in engine-api
45-
oldInfo := &v1p24.Info{
46-
InfoBase: info.InfoBase,
44+
type oldInfo struct {
45+
*types.Info
46+
ExecutionDriver string
47+
}
48+
old := &oldInfo{
49+
Info: info,
4750
ExecutionDriver: "<not supported>",
4851
}
49-
for _, s := range info.SecurityOptions {
50-
if s.Key == "Name" {
51-
oldInfo.SecurityOptions = append(oldInfo.SecurityOptions, s.Value)
52-
}
52+
nameOnlySecurityOptions := []string{}
53+
kvSecOpts, err := types.DecodeSecurityOptions(old.SecurityOptions)
54+
if err != nil {
55+
return err
56+
}
57+
for _, s := range kvSecOpts {
58+
nameOnlySecurityOptions = append(nameOnlySecurityOptions, s.Name)
5359
}
54-
return httputils.WriteJSON(w, http.StatusOK, oldInfo)
60+
old.SecurityOptions = nameOnlySecurityOptions
61+
return httputils.WriteJSON(w, http.StatusOK, old)
5562
}
5663
return httputils.WriteJSON(w, http.StatusOK, info)
5764
}

‎api/types/types.go

+43-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package types
22

33
import (
4+
"errors"
5+
"fmt"
46
"io"
57
"os"
8+
"strings"
69
"time"
710

811
"github.com/docker/docker/api/types/container"
@@ -158,9 +161,9 @@ type Commit struct {
158161
Expected string
159162
}
160163

161-
// InfoBase contains the base response of Remote API:
164+
// Info contains response of Remote API:
162165
// GET "/info"
163-
type InfoBase struct {
166+
type Info struct {
164167
ID string
165168
Containers int
166169
ContainersRunning int
@@ -219,18 +222,49 @@ type InfoBase struct {
219222
ContainerdCommit Commit
220223
RuncCommit Commit
221224
InitCommit Commit
225+
SecurityOptions []string
222226
}
223227

224-
// SecurityOpt holds key/value pair about a security option
225-
type SecurityOpt struct {
228+
// KeyValue holds a key/value pair
229+
type KeyValue struct {
226230
Key, Value string
227231
}
228232

229-
// Info contains response of Remote API:
230-
// GET "/info"
231-
type Info struct {
232-
*InfoBase
233-
SecurityOptions []SecurityOpt
233+
// SecurityOpt contains the name and options of a security option
234+
type SecurityOpt struct {
235+
Name string
236+
Options []KeyValue
237+
}
238+
239+
// DecodeSecurityOptions decodes a security options string slice to a type safe
240+
// SecurityOpt
241+
func DecodeSecurityOptions(opts []string) ([]SecurityOpt, error) {
242+
so := []SecurityOpt{}
243+
for _, opt := range opts {
244+
// support output from a < 1.13 docker daemon
245+
if !strings.Contains(opt, "=") {
246+
so = append(so, SecurityOpt{Name: opt})
247+
continue
248+
}
249+
secopt := SecurityOpt{}
250+
split := strings.Split(opt, ",")
251+
for _, s := range split {
252+
kv := strings.SplitN(s, "=", 2)
253+
if len(kv) != 2 {
254+
return nil, fmt.Errorf("invalid security option %q", s)
255+
}
256+
if kv[0] == "" || kv[1] == "" {
257+
return nil, errors.New("invalid empty security option")
258+
}
259+
if kv[0] == "name" {
260+
secopt.Name = kv[1]
261+
continue
262+
}
263+
secopt.Options = append(secopt.Options, KeyValue{Key: kv[0], Value: kv[1]})
264+
}
265+
so = append(so, secopt)
266+
}
267+
return so, nil
234268
}
235269

236270
// PluginsInfo is a temp struct holding Plugins name

‎api/types/versions/v1p24/types.go

-11
This file was deleted.

‎cli/command/system/info.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,21 @@ func prettyPrintInfo(dockerCli *command.DockerCli, info types.Info) error {
172172
fmt.Fprintf(dockerCli.Out(), "\n")
173173
}
174174
if len(info.SecurityOptions) != 0 {
175+
kvs, err := types.DecodeSecurityOptions(info.SecurityOptions)
176+
if err != nil {
177+
return err
178+
}
175179
fmt.Fprintf(dockerCli.Out(), "Security Options:\n")
176-
for _, o := range info.SecurityOptions {
177-
switch o.Key {
178-
case "Name":
179-
fmt.Fprintf(dockerCli.Out(), " %s\n", o.Value)
180-
case "Profile":
181-
if o.Value != "default" {
182-
fmt.Fprintf(dockerCli.Err(), " WARNING: You're not using the default seccomp profile\n")
180+
for _, so := range kvs {
181+
fmt.Fprintf(dockerCli.Out(), " %s\n", so.Name)
182+
for _, o := range so.Options {
183+
switch o.Key {
184+
case "profile":
185+
if o.Value != "default" {
186+
fmt.Fprintf(dockerCli.Err(), " WARNING: You're not using the default seccomp profile\n")
187+
}
188+
fmt.Fprintf(dockerCli.Out(), " Profile: %s\n", o.Value)
183189
}
184-
fmt.Fprintf(dockerCli.Out(), " %s: %s\n", o.Key, o.Value)
185190
}
186191
}
187192
}

‎client/info_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ func TestInfo(t *testing.T) {
4646
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
4747
}
4848
info := &types.Info{
49-
InfoBase: &types.InfoBase{
50-
ID: "daemonID",
51-
Containers: 3,
52-
},
49+
ID: "daemonID",
50+
Containers: 3,
5351
}
5452
b, err := json.Marshal(info)
5553
if err != nil {

‎daemon/info.go

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package daemon
22

33
import (
4+
"fmt"
45
"os"
56
"runtime"
67
"sync/atomic"
@@ -69,29 +70,26 @@ func (daemon *Daemon) SystemInfo() (*types.Info, error) {
6970
}
7071
})
7172

72-
securityOptions := []types.SecurityOpt{}
73+
securityOptions := []string{}
7374
if sysInfo.AppArmor {
74-
securityOptions = append(securityOptions, types.SecurityOpt{Key: "Name", Value: "apparmor"})
75+
securityOptions = append(securityOptions, "name=apparmor")
7576
}
7677
if sysInfo.Seccomp && supportsSeccomp {
7778
profile := daemon.seccompProfilePath
7879
if profile == "" {
7980
profile = "default"
8081
}
81-
securityOptions = append(securityOptions,
82-
types.SecurityOpt{Key: "Name", Value: "seccomp"},
83-
types.SecurityOpt{Key: "Profile", Value: profile},
84-
)
82+
securityOptions = append(securityOptions, fmt.Sprintf("name=seccomp,profile=%s", profile))
8583
}
8684
if selinuxEnabled() {
87-
securityOptions = append(securityOptions, types.SecurityOpt{Key: "Name", Value: "selinux"})
85+
securityOptions = append(securityOptions, "name=selinux")
8886
}
8987
uid, gid := daemon.GetRemappedUIDGID()
9088
if uid != 0 || gid != 0 {
91-
securityOptions = append(securityOptions, types.SecurityOpt{Key: "Name", Value: "userns"})
89+
securityOptions = append(securityOptions, "name=userns")
9290
}
9391

94-
v := &types.InfoBase{
92+
v := &types.Info{
9593
ID: daemon.ID,
9694
Containers: int(cRunning + cPaused + cStopped),
9795
ContainersRunning: int(cRunning),
@@ -129,6 +127,7 @@ func (daemon *Daemon) SystemInfo() (*types.Info, error) {
129127
HTTPSProxy: sockets.GetProxyEnv("https_proxy"),
130128
NoProxy: sockets.GetProxyEnv("no_proxy"),
131129
LiveRestoreEnabled: daemon.configStore.LiveRestoreEnabled,
130+
SecurityOptions: securityOptions,
132131
Isolation: daemon.defaultIsolation,
133132
}
134133

@@ -143,12 +142,7 @@ func (daemon *Daemon) SystemInfo() (*types.Info, error) {
143142
}
144143
v.Name = hostname
145144

146-
i := &types.Info{
147-
InfoBase: v,
148-
SecurityOptions: securityOptions,
149-
}
150-
151-
return i, nil
145+
return v, nil
152146
}
153147

154148
// SystemVersion returns version information about the daemon.

‎daemon/info_unix.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
// FillPlatformInfo fills the platform related info.
17-
func (daemon *Daemon) FillPlatformInfo(v *types.InfoBase, sysInfo *sysinfo.SysInfo) {
17+
func (daemon *Daemon) FillPlatformInfo(v *types.Info, sysInfo *sysinfo.SysInfo) {
1818
v.MemoryLimit = sysInfo.MemoryLimit
1919
v.SwapLimit = sysInfo.SwapLimit
2020
v.KernelMemory = sysInfo.KernelMemory

‎daemon/info_windows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ import (
66
)
77

88
// FillPlatformInfo fills the platform related info.
9-
func (daemon *Daemon) FillPlatformInfo(v *types.InfoBase, sysInfo *sysinfo.SysInfo) {
9+
func (daemon *Daemon) FillPlatformInfo(v *types.Info, sysInfo *sysinfo.SysInfo) {
1010
}

0 commit comments

Comments
 (0)
Please sign in to comment.