| 1 | // Copyright 2023 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Package faketsm defines a configfsi.Client for faking TSM behavior. |
| 16 | // The provider attribute returns "fake" and the attestation report format |
| 17 | // is just the state of the attributes. The certificate blob is part of the |
| 18 | // Client definition. |
| 19 | package faketsmscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/ |
| 20 | |
| 21 | import ( |
| 22 | "fmtscip-go gomod github.com/golang/go/src go1.19 fmt/" |
| 23 | "osscip-go gomod github.com/golang/go/src go1.19 os/" |
| 24 | |
| 25 | "github.com/google/go-configfs-tsm/configfs/configfsiscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/configfsi`/" |
| 26 | ) |
| 27 | |
| 28 | // Client provides a "fake" provider for configfs to emulate the /sys/kernel/config/tsm behavior. |
| 29 | // Dispatches to specialized subsystem Client interfaces. |
| 30 | type Clientscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client# struct { |
| 31 | Subsystemsscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#Subsystems. map[string]configfsiscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/configfsi`/.Clientscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/configfsi`/Client# |
| 32 | } |
| 33 | |
| 34 | func (c *Clientscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#) getSubsystemscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#getSubsystem().(name string) (configfsiscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/configfsi`/.Clientscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/configfsi`/Client#, error) { |
| 35 | p, err := configfsiscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/configfsi`/.ParseTsmPathscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/configfsi`/ParseTsmPath().(name) |
| 36 | if err != nil { |
| 37 | return nil, fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("getSubsystem: %v", err) |
| 38 | } |
| 39 | if p.Subsystemscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/configfsi`/TsmPath#Subsystem. == "" { |
| 40 | return nil, fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("faketsm: expected tsm subsystem in %q", name) |
| 41 | } |
| 42 | sub, ok := c.Subsystemsscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#Subsystems.[p.Subsystemscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/configfsi`/TsmPath#Subsystem.] |
| 43 | if !ok { |
| 44 | return nil, fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("faketsm: unsupported subsystem %q", p.Subsystemscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/configfsi`/TsmPath#Subsystem.) |
| 45 | } |
| 46 | return sub, nil |
| 47 | } |
| 48 | |
| 49 | // ReadDir reads the directory named by dir and returns a list of directory entries. |
| 50 | func (c *Clientscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#) ReadDirscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#ReadDir().(dir string) ([]osscip-go gomod github.com/golang/go/src go1.19 os/.DirEntryscip-go gomod github.com/golang/go/src go1.19 os/DirEntry#, error) { |
| 51 | if dir == "" { |
| 52 | return nil, fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("faketsm doesn't implement empty directory behavior") |
| 53 | } |
| 54 | sub, err := c.getSubsystemscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#getSubsystem().(dir) |
| 55 | if err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | return sub.ReadDirscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/configfsi`/Client#ReadDir.(dir) |
| 59 | } |
| 60 | |
| 61 | // MkdirTemp creates a new temporary directory in the directory dir and returns the pathname |
| 62 | // of the new directory. Pattern semantics follow os.MkdirTemp. |
| 63 | func (c *Clientscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#) MkdirTempscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#MkdirTemp().(dir, pattern string) (string, error) { |
| 64 | if dir == "" { |
| 65 | return "", fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("faketsm doesn't implement empty directory behavior") |
| 66 | } |
| 67 | sub, err := c.getSubsystemscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#getSubsystem().(dir) |
| 68 | if err != nil { |
| 69 | return "", err |
| 70 | } |
| 71 | return sub.MkdirTempscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/configfsi`/Client#MkdirTemp.(dir, pattern) |
| 72 | } |
| 73 | |
| 74 | // ReadFile reads the named file and returns the contents. |
| 75 | func (c *Clientscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#) ReadFilescip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#ReadFile().(name string) ([]byte, error) { |
| 76 | sub, err := c.getSubsystemscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#getSubsystem().(name) |
| 77 | if err != nil { |
| 78 | return nil, err |
| 79 | } |
| 80 | return sub.ReadFilescip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/configfsi`/Client#ReadFile.(name) |
| 81 | } |
| 82 | |
| 83 | // WriteFile writes data to the named file, creating it if necessary. The permissions |
| 84 | // are implementation-defined. |
| 85 | func (c *Clientscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#) WriteFilescip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#WriteFile().(name string, contents []byte) error { |
| 86 | sub, err := c.getSubsystemscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#getSubsystem().(name) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | return sub.WriteFilescip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/configfsi`/Client#WriteFile.(name, contents) |
| 91 | } |
| 92 | |
| 93 | // RemoveAll removes path and any children it contains. |
| 94 | func (c *Clientscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#) RemoveAllscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#RemoveAll().(name string) error { |
| 95 | sub, err := c.getSubsystemscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/faketsm`/Client#getSubsystem().(name) |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | return sub.RemoveAllscip-go gomod github.com/google/go-configfs-tsm . `github.com/google/go-configfs-tsm/configfs/configfsi`/Client#RemoveAll.(name) |
| 100 | } |