|
| 1 | +// Copyright 2022 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 | +// https://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 proxy |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "syscall" |
| 20 | + |
| 21 | + "github.com/hanwen/go-fuse/v2/fs" |
| 22 | + "github.com/hanwen/go-fuse/v2/fuse" |
| 23 | + "github.com/hanwen/go-fuse/v2/fuse/nodefs" |
| 24 | +) |
| 25 | + |
| 26 | +// symlink implements a symbolic link, returning the underlying path when |
| 27 | +// Readlink is called. |
| 28 | +type symlink struct { |
| 29 | + fs.Inode |
| 30 | + path string |
| 31 | +} |
| 32 | + |
| 33 | +// Readlink implements fs.NodeReadlinker and returns the symlink's path. |
| 34 | +func (s *symlink) Readlink(ctx context.Context) ([]byte, syscall.Errno) { |
| 35 | + return []byte(s.path), fs.OK |
| 36 | +} |
| 37 | + |
| 38 | +// readme represents a static read-only text file. |
| 39 | +type readme struct { |
| 40 | + fs.Inode |
| 41 | +} |
| 42 | + |
| 43 | +const readmeText = ` |
| 44 | +When applications attempt to open files in this directory, a remote connection |
| 45 | +to the Cloud SQL instance of the same name will be established. |
| 46 | +
|
| 47 | +For example, when you run one of the followg commands, the proxy will initiate a |
| 48 | +connection to the corresponding Cloud SQL instance, given you have the correct |
| 49 | +IAM permissions. |
| 50 | +
|
| 51 | + mysql -u root -S "/somedir/project:region:instance" |
| 52 | +
|
| 53 | + # or |
| 54 | +
|
| 55 | + psql "host=/somedir/project:region:instance dbname=mydb user=myuser" |
| 56 | +
|
| 57 | +For MySQL, the proxy will create a socket with the instance connection name |
| 58 | +(e.g., project:region:instance) in this directory. For Postgres, the proxy will |
| 59 | +create a directory with the instance connection name, and create a socket inside |
| 60 | +that directory with the special Postgres name: .s.PGSQL.5432. |
| 61 | +
|
| 62 | +Listing the contents of this directory will show all instances with active |
| 63 | +connections. |
| 64 | +` |
| 65 | + |
| 66 | +// Getattr implements fs.NodeGetattrer and indicates that this file is a regular |
| 67 | +// file. |
| 68 | +func (*readme) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno { |
| 69 | + *out = fuse.AttrOut{Attr: fuse.Attr{ |
| 70 | + Mode: 0444 | syscall.S_IFREG, |
| 71 | + Size: uint64(len(readmeText)), |
| 72 | + }} |
| 73 | + return fs.OK |
| 74 | +} |
| 75 | + |
| 76 | +// Read implements fs.NodeReader and supports incremental reads. |
| 77 | +func (*readme) Read(ctx context.Context, f fs.FileHandle, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) { |
| 78 | + end := int(off) + len(dest) |
| 79 | + if end > len(readmeText) { |
| 80 | + end = len(readmeText) |
| 81 | + } |
| 82 | + return fuse.ReadResultData([]byte(readmeText[off:end])), fs.OK |
| 83 | +} |
| 84 | + |
| 85 | +// Open implements fs.NodeOpener and supports opening the README as a read-only |
| 86 | +// file. |
| 87 | +func (*readme) Open(ctx context.Context, mode uint32) (fs.FileHandle, uint32, syscall.Errno) { |
| 88 | + df := nodefs.NewDataFile([]byte(readmeText)) |
| 89 | + rf := nodefs.NewReadOnlyFile(df) |
| 90 | + return rf, 0, fs.OK |
| 91 | +} |
0 commit comments