diff --git a/Dockerfile b/Dockerfile index c23c0865485..12c19d27f5f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,7 +25,7 @@ COPY vendor/ vendor/ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager sigs.k8s.io/cluster-api-provider-azure/cmd/manager # Copy the controller-manager into a thin image -FROM alpine:3.8 +FROM alpine:3.9 WORKDIR /root/ COPY --from=builder /go/src/sigs.k8s.io/cluster-api-provider-azure/manager . COPY --from=builder /go/src/sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/resources/template/deployment-template.json . diff --git a/Gopkg.lock b/Gopkg.lock index 2a11e6b26ba..fe749c3d988 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -95,15 +95,15 @@ version = "v1.1.0" [[projects]] - digest = "1:899234af23e5793c34e06fd397f86ba33af5307b959b6a7afd19b63db065a9d7" + digest = "1:8ee7b41ace3ba875c17e38ba7780e7cf0d29882338637861e9f13f04f60ecc5c" name = "github.com/emicklei/go-restful" packages = [ ".", "log", ] pruneopts = "UT" - revision = "3eb9738c1697594ea6e71a7156a9bb32ed216cf0" - version = "v2.8.0" + revision = "85d198d05a92d31823b852b4a5928114912e8949" + version = "v2.9.0" [[projects]] digest = "1:2cd7915ab26ede7d95b8749e6b1f933f1c6d5398030684e6505940a10f31cfda" @@ -130,39 +130,31 @@ version = "v0.1.0" [[projects]] - digest = "1:9d89edd2ba8f85df7735cd89dd5d76dfb9672c85fb813f5bfaca654e026be806" + digest = "1:5b33a8ff87888677489ba612c8aa1bf06500b087f1397a4732b7a0298f6d5f8a" name = "github.com/gobuffalo/envy" packages = ["."] pruneopts = "UT" - revision = "910ef88c9d32c6e779231577dfcf6ed8959bea2f" - version = "v1.6.8" + revision = "fa0dfdc10b5366ce365b7d9d1755a03e4e797bc5" + version = "v1.6.15" [[projects]] - digest = "1:34e709f36fd4f868fb00dbaf8a6cab4c1ae685832d392874ba9d7c5dec2429d1" + digest = "1:b402bb9a24d108a9405a6f34675091b036c8b056aac843bf6ef2389a65c5cf48" name = "github.com/gogo/protobuf" packages = [ "proto", "sortkeys", ] pruneopts = "UT" - revision = "636bf0302bc95575d69441b25a2603156ffdddf1" - version = "v1.1.1" - -[[projects]] - branch = "master" - digest = "1:1ba1d79f2810270045c328ae5d674321db34e3aae468eb4233883b473c5c0467" - name = "github.com/golang/glog" - packages = ["."] - pruneopts = "UT" - revision = "23def4e6c14b4da8ac2ed8007337bc5eb5007998" + revision = "4cbf7e384e768b4e01799441fdf2a706a5635ae7" + version = "v1.2.0" [[projects]] branch = "master" - digest = "1:3fb07f8e222402962fa190eb060608b34eddfb64562a18e2167df2de0ece85d8" + digest = "1:b7cb6054d3dff43b38ad2e92492f220f57ae6087ee797dca298139776749ace8" name = "github.com/golang/groupcache" packages = ["lru"] pruneopts = "UT" - revision = "c65c006176ff7ff98bb916961c7abbc6b0afc0aa" + revision = "5b532d6fd5efaf7fa130d4e859a2fde0fc3a9e1b" [[projects]] digest = "1:8f0705fa33e8957018611cc81c65cb373b626c092d39931bb86882489fc4c3f4" @@ -196,12 +188,12 @@ revision = "24818f796faf91cd76ec7bddd72458fbced7a6c1" [[projects]] - digest = "1:3a26588bc48b96825977c1b3df964f8fd842cd6860cc26370588d3563433cf11" + digest = "1:236d7e1bdb50d8f68559af37dbcf9d142d56b431c9b2176d41e2a009b664cda8" name = "github.com/google/uuid" packages = ["."] pruneopts = "UT" - revision = "d460ce9f8df2e77fb1ba55ca87fafed96c607494" - version = "v1.0.0" + revision = "9b3b1e0f5f99ae461456d768e7d301a7acdaa2d8" + version = "v1.1.0" [[projects]] digest = "1:65c4414eeb350c47b8de71110150d0ea8a281835b1f386eacaa3ad7325929c21" @@ -217,14 +209,14 @@ [[projects]] branch = "master" - digest = "1:86c1210529e69d69860f2bb3ee9ccce0b595aa3f9165e7dd1388e5c612915888" + digest = "1:b4395b2a4566c24459af3d04009b39cc21762fc77ec7bf7a1aa905c91e8f018d" name = "github.com/gregjones/httpcache" packages = [ ".", "diskcache", ] pruneopts = "UT" - revision = "c63ab54fda8f77302f8d414e19933f2b6026a089" + revision = "3befbb6ad0cc97d4c25d851e9528915809e1a22f" [[projects]] digest = "1:8ec8d88c248041a6df5f6574b87bc00e7e0b493881dad2e7ef47b11dc69093b5" @@ -252,12 +244,12 @@ version = "v1.0.0" [[projects]] - digest = "1:8eb1de8112c9924d59bf1d3e5c26f5eaa2bfc2a5fcbb92dc1c2e4546d695f277" + digest = "1:a0cefd27d12712af4b5018dc7046f245e1e3b5760e2e848c30b171b570708f9b" name = "github.com/imdario/mergo" packages = ["."] pruneopts = "UT" - revision = "9f23e2d6bd2a77f959b2bf6acdbefd708a83a4a4" - version = "v0.3.6" + revision = "7c29201646fa3de8506f701213473dd407f19646" + version = "v0.3.7" [[projects]] digest = "1:870d441fe217b8e689d7949fef6e43efbc787e50f200cb1e70dbca9204a1d6be" @@ -283,14 +275,6 @@ revision = "1624edc4454b8682399def8740d46db5e4362ba4" version = "v1.1.5" -[[projects]] - digest = "1:9cedee824c21326bd26950bd9e1ffe9dc4e7ca03dc8634d0e6f954ee6a383172" - name = "github.com/kr/fs" - packages = ["."] - pruneopts = "UT" - revision = "1455def202f6e05b95cc7bfc7e8ae67ae5141eba" - version = "v0.1.0" - [[projects]] digest = "1:3804a3a02964db8e6db3e5e7960ac1c1a9b12835642dd4f4ac4e56c749ec73eb" name = "github.com/markbates/inflect" @@ -308,12 +292,12 @@ version = "v1.0.1" [[projects]] - digest = "1:78bbb1ba5b7c3f2ed0ea1eab57bdd3859aec7e177811563edc41198a760b06af" + digest = "1:5d231480e1c64a726869bc4142d270184c419749d34f167646baa21008eb0a79" name = "github.com/mitchellh/go-homedir" packages = ["."] pruneopts = "UT" - revision = "ae18d6b8b3205b561c79e8e5f69bff09736185f4" - version = "v1.0.0" + revision = "af06845cf3004701891bf4fdb884bfe4920b3727" + version = "v1.1.0" [[projects]] digest = "1:33422d238f147d247752996a26574ac48dcf472976eda7f5134015f06bf16563" @@ -332,7 +316,7 @@ version = "1.0.1" [[projects]] - digest = "1:42e29deef12327a69123b9cb2cb45fee4af5c12c2a23c6e477338279a052703f" + digest = "1:5f4b78246f0bcb105b1e3b2b9e22b52a57cd02f57a8078572fe27c62f4a75ff7" name = "github.com/onsi/ginkgo" packages = [ ".", @@ -355,11 +339,11 @@ "types", ] pruneopts = "UT" - revision = "3774a09d95489ccaa16032e0770d08ea77ba6184" - version = "v1.6.0" + revision = "2e1be8f7d90e9d3e3e58b0ce470f2f14d075406f" + version = "v1.7.0" [[projects]] - digest = "1:d6d3014e71a3504f7fda993889cfdc09ef5b70b46b00dd6bd078943585944352" + digest = "1:b4764603c54d74435f246901248aefb2b9d430bb7b160afde1afc41d89d48f1a" name = "github.com/onsi/gomega" packages = [ ".", @@ -378,8 +362,8 @@ "types", ] pruneopts = "UT" - revision = "7615b9433f86a8bdf29709bf288bc4fd0636a369" - version = "v1.4.2" + revision = "65fb64232476ad9046e57c26cd0bff3d3a8dc6cd" + version = "v1.4.3" [[projects]] digest = "1:e5d0bd87abc2781d14e274807a470acd180f0499f8bf5bb18606e9ec22ad9de9" @@ -406,20 +390,12 @@ version = "v2.0.1" [[projects]] - digest = "1:40e195917a951a8bf867cd05de2a46aaf1806c50cf92eebf4c16f78cd196f747" + digest = "1:cf31692c14422fa27c83a05292eb5cbe0fb2775972e8f1f8446a71549bd8980b" name = "github.com/pkg/errors" packages = ["."] pruneopts = "UT" - revision = "645ef00459ed84a119197bfb8d8205042c6df63d" - version = "v0.8.0" - -[[projects]] - digest = "1:98c46dbde8c257608669d6827aacb8c8722d6ea7b4ffd7c4d1ea32ead21bfea9" - name = "github.com/pkg/sftp" - packages = ["."] - pruneopts = "UT" - revision = "08de04f133f27844173471167014e1a753655ac8" - version = "v1.8.3" + revision = "ba968bfe8b2f7e042a574c888954fccecfa385b4" + version = "v0.8.1" [[projects]] digest = "1:93a746f1060a8acbcf69344862b2ceced80f854170e1caae089b2834c5fbf7f4" @@ -467,15 +443,27 @@ revision = "f8d8b3f739bd91a7c0462cb55235ef63c79c9abc" [[projects]] - digest = "1:6a4a11ba764a56d2758899ec6f3848d24698d48442ebce85ee7a3f63284526cd" + digest = "1:e09ada96a5a41deda4748b1659cc8953961799e798aea557257b56baee4ecaf3" + name = "github.com/rogpeppe/go-internal" + packages = [ + "modfile", + "module", + "semver", + ] + pruneopts = "UT" + revision = "1cf9852c553c5b7da2d5a4a091129a7822fed0c9" + version = "v1.2.2" + +[[projects]] + digest = "1:3e39bafd6c2f4bf3c76c3bfd16a2e09e016510ad5db90dc02b88e2f565d6d595" name = "github.com/spf13/afero" packages = [ ".", "mem", ] pruneopts = "UT" - revision = "d40851caa0d747393da1ffb28f7f9d8b4eeffebd" - version = "v1.1.2" + revision = "f4711e4db9e9a1d3887343acb72b2bbfc2f686f5" + version = "v1.2.1" [[projects]] digest = "1:645cabccbb4fa8aab25a956cbcbdf6a6845ca736b2c64e197ca7cbb9d210b939" @@ -550,7 +538,7 @@ [[projects]] branch = "master" - digest = "1:9bca23e99b8dc02f7f22d197ee886acde6dd7c7a16855f1822ed78f362c2ff1b" + digest = "1:11984fc97cca1b53d4fe77822c7e44acfadaea7985973772181d3698ef655df4" name = "golang.org/x/crypto" packages = [ "curve25519", @@ -565,11 +553,11 @@ "ssh/terminal", ] pruneopts = "UT" - revision = "3d3f9f413869b949e48070b5bc593aa22cc2b8f2" + revision = "74369b46fc6756741c016591724fd1cb8e26845f" [[projects]] branch = "master" - digest = "1:3b26c934d4b1b601aa272456290a18144b072259c44e92040a1925fec6ed7013" + digest = "1:23fcc2e5f93d50effadf8cfbad225ff50e7e4ea64cec647d2765649cff4ed4f2" name = "golang.org/x/net" packages = [ "context", @@ -585,7 +573,7 @@ "trace", ] pruneopts = "UT" - revision = "88d92db4c548972d942ac2a3531a8a9a34c82ca6" + revision = "3a22650c66bd7f4fb6d1e8072ffd7b75c8a27898" [[projects]] branch = "master" @@ -608,14 +596,14 @@ [[projects]] branch = "master" - digest = "1:6a875550c3b582f6c2d7e2ce44aba792511f00016d7c46b0a4fb26f730ef3058" + digest = "1:0d703f14f9bbbe1070ff0ce86d749dcbc9d68fb0ae554252c09bd4bb37a072e7" name = "golang.org/x/sys" packages = [ "unix", "windows", ] pruneopts = "UT" - revision = "66b7b1311ac80bbafcd2daeef9a5e6e2cd1e2399" + revision = "d0b11bdaac8adb652bff00e49bcacf992835621a" [[projects]] digest = "1:436b24586f8fee329e0dd65fd67c817681420cda1d7f934345c13fe78c212a73" @@ -662,24 +650,32 @@ [[projects]] branch = "master" - digest = "1:6511064c08bff306dd24fa83ea82e160009fa7572240b70173b25d1dbc3e1872" + digest = "1:f57e88c4326d5433c2f4eb28fcd1662be003cb0d11dd13710c8828e687303c76" name = "golang.org/x/tools" packages = [ "go/ast/astutil", + "go/gcexportdata", + "go/internal/cgo", + "go/internal/gcimporter", + "go/internal/packagesdriver", + "go/packages", + "go/types/typeutil", "imports", "internal/fastwalk", "internal/gopathwalk", + "internal/module", + "internal/semver", ] pruneopts = "UT" - revision = "7e59e591a2617a3067db5fe2fe8e903b4c1b10cc" + revision = "8dcb7bc8c7fe0a895995c76c721cef79419ac98a" [[projects]] - branch = "master" digest = "1:5f003878aabe31d7f6b842d4de32b41c46c214bb629bb485387dbcce1edf5643" name = "google.golang.org/api" packages = ["support/bundler"] pruneopts = "UT" - revision = "f26a60c56f148a32e87f3f4591c8ebf834b5561f" + revision = "19e022d8cf43ce81f046bae8cc18c5397cc7732f" + version = "v0.1.0" [[projects]] digest = "1:6f3bd49ddf2e104e52062774d797714371fac1b8bddfd8e124ce78e6b2264a10" @@ -703,10 +699,10 @@ name = "google.golang.org/genproto" packages = ["googleapis/rpc/status"] pruneopts = "UT" - revision = "bd9b4fb69e2ffd37621a6caa54dcbead29b546f2" + revision = "bd968387e4aa590b3c6e4ba38ecac1ed314d43ad" [[projects]] - digest = "1:9edd250a3c46675d0679d87540b30c9ed253b19bd1fd1af08f4f5fb3c79fc487" + digest = "1:9ab5a33d8cb5c120602a34d2e985ce17956a4e8c2edce7e6961568f95e40c09a" name = "google.golang.org/grpc" packages = [ ".", @@ -742,8 +738,8 @@ "tap", ] pruneopts = "UT" - revision = "df014850f6dee74ba2fc94874043a9f3f75fbfd8" - version = "v1.17.0" + revision = "a02b0774206b209466313a0b525d2c738fe407eb" + version = "v1.18.0" [[projects]] digest = "1:abeb38ade3f32a92943e5be54f55ed6d6e3b6602761d74b4aab4c9dd45c18abd" @@ -771,12 +767,12 @@ revision = "dd632973f1e7218eb1089048e0798ec9ae7dceb8" [[projects]] - digest = "1:342378ac4dcb378a5448dd723f0784ae519383532f5e70ade24132c4c8693202" + digest = "1:4d2e5a73dc1500038e504a8d78b986630e3626dc027bc030ba5c75da257cdb96" name = "gopkg.in/yaml.v2" packages = ["."] pruneopts = "UT" - revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183" - version = "v2.2.1" + revision = "51d6538a90f86fe93ac480b35f37b2be17fef232" + version = "v2.2.2" [[projects]] digest = "1:3ca4baec1a14e727951288f091badc5d403096d1c00d3ff5299f273a0c6db85d" @@ -821,15 +817,15 @@ version = "kubernetes-1.13.1" [[projects]] - digest = "1:b79f62bd5d9766441c4c62ff4b23dea0fdc8b40054cdbc06d64c3cfb4aee020f" + digest = "1:46e3296eec0a6e864c2ef813ccfdb0b83d144fcc23414d97dd4894e9464cf4d5" name = "k8s.io/apiextensions-apiserver" packages = [ "pkg/apis/apiextensions", "pkg/apis/apiextensions/v1beta1", ] pruneopts = "UT" - revision = "408db4a50408e2149acbd657bceb2480c13cb0a4" - version = "kubernetes-1.11.2" + revision = "0fe22c71c47604641d9aa352c785b7912c200562" + version = "kubernetes-1.13.1" [[projects]] digest = "1:cded8487c675439a87cc6a3462ec26d2dd78362a2d89c484f58b1d6c181fb877" @@ -958,6 +954,17 @@ revision = "8d9ed539ba3134352c586810e749e58df4e94e4f" version = "kubernetes-1.13.1" +[[projects]] + branch = "master" + digest = "1:28fd6a61b5220aab30c6f7344676ff5aaea4a5f14407d95f33e238b13da2d2ef" + name = "k8s.io/cluster-bootstrap" + packages = [ + "token/api", + "token/util", + ] + pruneopts = "UT" + revision = "4f06c581c6d8fd832680b9614932657955ad8a05" + [[projects]] branch = "master" digest = "1:937e46834bfd618b223206d7e3d459bf0eb53ac7d05135bf809d73b8fafdf7f7" @@ -968,11 +975,11 @@ "pkg/util", ] pruneopts = "UT" - revision = "1ae8250a766a78be82efa54a48139ea3148c9c08" + revision = "095ce2f23e83e3ee87fb5ff612fabb05e9df0155" [[projects]] branch = "master" - digest = "1:4db88b0d181fd2736b151f06eda6c776ef9ff2262d00d0eb5d6c947c03b00e91" + digest = "1:28514fabca4356625720ffb012408790a9d00d31963a9bd9daf7b5ccd894c301" name = "k8s.io/gengo" packages = [ "args", @@ -984,15 +991,15 @@ "types", ] pruneopts = "UT" - revision = "fd15ee9cc2f77baa4f31e59e6acbf21146455073" + revision = "0689ccc1d7d65d9dd1bedcc3b0b1ed7df91ba266" [[projects]] - digest = "1:e2999bf1bb6eddc2a6aa03fe5e6629120a53088926520ca3b4765f77d7ff7eab" + digest = "1:72fd56341405f53c745377e0ebc4abeff87f1a048e0eea6568a20212650f5a82" name = "k8s.io/klog" packages = ["."] pruneopts = "UT" - revision = "a5bc97fbc634d635061f3146511332c7e313a55a" - version = "v0.1.0" + revision = "71442cd4037d612096940ceb0f3fec3f7fff66e0" + version = "v0.2.0" [[projects]] branch = "master" @@ -1000,7 +1007,7 @@ name = "k8s.io/kube-openapi" packages = ["pkg/util/proto"] pruneopts = "UT" - revision = "c59034cc13d587f5ef4e85ca0ade0c1866ae8e1d" + revision = "ea82251f3668f8c1bde607fa6e20e5bf36e576a4" [[projects]] digest = "1:0dd3be513e09375e3f8fefc718fdfc1aac6924dc57adecb6dddfc7edc96a8dda" @@ -1034,7 +1041,7 @@ revision = "0f315e945d5fabe0c805ccf30fa6cf02fac6c0b5" [[projects]] - digest = "1:b7ca84cccb4630cbd53eef277cf73ac87d7bbcc9ae3e72b8cf414be381853dcc" + digest = "1:743eebdcc3e564ed574b3fa0c10c223f5edeba6ee2e3c625c345c1604432a8f1" name = "sigs.k8s.io/controller-runtime" packages = [ "pkg/cache", @@ -1061,8 +1068,13 @@ "pkg/runtime/signals", "pkg/source", "pkg/source/internal", + "pkg/webhook", "pkg/webhook/admission", "pkg/webhook/admission/types", + "pkg/webhook/internal/cert", + "pkg/webhook/internal/cert/generator", + "pkg/webhook/internal/cert/writer", + "pkg/webhook/internal/cert/writer/atomic", "pkg/webhook/internal/metrics", "pkg/webhook/types", ] @@ -1071,31 +1083,35 @@ version = "v0.1.10" [[projects]] - branch = "revert-63-k8s-1.10-with-clean-deps" - digest = "1:99a107c3273c01fed11376b7e718582979544dba68f27ceec99d7967fa612855" + digest = "1:992675a6714d511089a0b7ffb7063d36e5423089cda610642de7a0cfbbf673ab" name = "sigs.k8s.io/controller-tools" packages = [ "cmd/controller-gen", "pkg/crd/generator", "pkg/crd/util", - "pkg/generate/rbac", "pkg/internal/codegen", "pkg/internal/codegen/parse", + "pkg/internal/general", + "pkg/rbac", "pkg/util", + "pkg/webhook", + "pkg/webhook/internal", ] pruneopts = "UT" - revision = "999adc0c9bd4ce50108fe91d104557f1d455f4f6" + revision = "fbf141159251d035089e7acdd5a343f8cec91b94" + version = "v0.1.9" [[projects]] - branch = "master" - digest = "1:fcda4907e877328a34cafaa567073836809d79f43027d6ed9a036758affdcb6c" + digest = "1:9070222ca967d09b3966552a161dd4420d62315964bf5e1efd8cc4c7c30ebca8" name = "sigs.k8s.io/testing_frameworks" packages = [ "integration", + "integration/addr", "integration/internal", ] pruneopts = "UT" - revision = "5818a3a284a11812aaed11d5ca0bcadec2c50e83" + revision = "d348cb12705b516376e0c323bacca72b00a78425" + version = "v0.1.1" [[projects]] digest = "1:7719608fe0b52a4ece56c2dde37bedd95b938677d1ab0f84b8a7852e4c59f849" @@ -1119,7 +1135,6 @@ "github.com/onsi/ginkgo", "github.com/onsi/gomega", "github.com/pkg/errors", - "github.com/pkg/sftp", "github.com/spf13/cobra", "golang.org/x/crypto/ssh", "k8s.io/api/core/v1", @@ -1128,10 +1143,14 @@ "k8s.io/apimachinery/pkg/runtime", "k8s.io/apimachinery/pkg/runtime/schema", "k8s.io/apimachinery/pkg/util/json", + "k8s.io/apimachinery/pkg/util/wait", "k8s.io/client-go/kubernetes", + "k8s.io/client-go/kubernetes/typed/core/v1", "k8s.io/client-go/tools/clientcmd", "k8s.io/client-go/tools/clientcmd/api", "k8s.io/client-go/tools/record", + "k8s.io/cluster-bootstrap/token/api", + "k8s.io/cluster-bootstrap/token/util", "k8s.io/code-generator/cmd/deepcopy-gen", "k8s.io/klog", "sigs.k8s.io/cluster-api/cmd/clusterctl/clientcmd", @@ -1142,6 +1161,7 @@ "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset", "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1", "sigs.k8s.io/cluster-api/pkg/controller/cluster", + "sigs.k8s.io/cluster-api/pkg/controller/error", "sigs.k8s.io/cluster-api/pkg/controller/machine", "sigs.k8s.io/controller-runtime/pkg/client/config", "sigs.k8s.io/controller-runtime/pkg/controller", diff --git a/Makefile b/Makefile index 1e7f435c345..446b30d7a55 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ FASTBUILD ?= n ## Set FASTBUILD=y (case-sensitive) to skip some slow tasks ## Image URL to use all building/pushing image targets STABLE_DOCKER_REPO ?= quay.io/k8s MANAGER_IMAGE_NAME ?= cluster-api-azure-controller -MANAGER_IMAGE_TAG ?= 0.2.0-alpha.4 +MANAGER_IMAGE_TAG ?= 0.1.0-alpha.3 MANAGER_IMAGE ?= $(STABLE_DOCKER_REPO)/$(MANAGER_IMAGE_NAME):$(MANAGER_IMAGE_TAG) DEV_DOCKER_REPO ?= quay.io/k8s DEV_MANAGER_IMAGE ?= $(DEV_DOCKER_REPO)/$(MANAGER_IMAGE_NAME):$(MANAGER_IMAGE_TAG)-dev @@ -114,6 +114,11 @@ BAZEL_DOCKER_ARGS_DEV := --define=DOCKER_REPO=$(DEV_DOCKER_REPO) $(BAZEL_DOCKER_ .PHONY: docker-build docker-build: generate ## Build the production docker image + docker build . -t $(MANAGER_IMAGE) + +# TODO: Move this to docker-build target once we figure out multi-stage builds and using a thinner image +.PHONY: docker-build-new +docker-build-new: generate ## Build the production docker image bazel run //cmd/manager:manager-image $(BAZEL_DOCKER_ARGS) .PHONY: docker-build-dev @@ -122,6 +127,11 @@ docker-build-dev: generate ## Build the development docker image .PHONY: docker-push docker-push: generate ## Push production docker image + docker push $(MANAGER_IMAGE) + +# TODO: Move this to docker-push target once we figure out multi-stage builds and using a thinner image +.PHONY: docker-push-new +docker-push-new: generate ## Push production docker image bazel run //cmd/manager:manager-push $(BAZEL_DOCKER_ARGS) .PHONY: docker-push-dev @@ -157,7 +167,7 @@ examples-dev: ## Generate example output with developer image .PHONY: manifests manifests: #cmd/clusterctl/examples/azure/out/credentials ## Generate manifests for clusterctl - go run vendor/sigs.k8s.io/controller-tools/cmd/controller-gen/main.go crd + go run vendor/sigs.k8s.io/controller-tools/cmd/controller-gen/main.go all kustomize build config/default/ > cmd/clusterctl/examples/azure/out/provider-components.yaml echo "---" >> cmd/clusterctl/examples/azure/out/provider-components.yaml kustomize build vendor/sigs.k8s.io/cluster-api/config/default/ >> cmd/clusterctl/examples/azure/out/provider-components.yaml diff --git a/cmd/clusterctl/examples/azure/generate-yaml.sh b/cmd/clusterctl/examples/azure/generate-yaml.sh index 7e3bab243c1..e3146a33a5a 100755 --- a/cmd/clusterctl/examples/azure/generate-yaml.sh +++ b/cmd/clusterctl/examples/azure/generate-yaml.sh @@ -25,14 +25,14 @@ ENVSUBST=${ENVSUBST:-envsubst} RANDOM_STRING=$(date | md5sum | head -c8) # Azure settings. -export LOCATION="${LOCATION:-eastus}" +export LOCATION="${LOCATION:-eastus2}" export RESOURCE_GROUP="${RESOURCE_GROUP:-capi-${RANDOM_STRING}}" # Cluster name. export CLUSTER_NAME="${CLUSTER_NAME:-test1}" # Manager image. -export MANAGER_IMAGE="${MANAGER_IMAGE:-quay.io/k8s/cluster-api-azure-controller:0.2.0-alpha.4}" +export MANAGER_IMAGE="${MANAGER_IMAGE:-quay.io/k8s/cluster-api-azure-controller:0.1.0-alpha.3}" export MANAGER_IMAGE_PULL_POLICY=${MANAGER_IMAGE_PULL_POLICY:-IfNotPresent} # Machine settings. diff --git a/cmd/clusterctl/examples/azure/machines.yaml.template b/cmd/clusterctl/examples/azure/machines.yaml.template index 2fa5f9049ac..723321de78e 100644 --- a/cmd/clusterctl/examples/azure/machines.yaml.template +++ b/cmd/clusterctl/examples/azure/machines.yaml.template @@ -10,8 +10,8 @@ items: set: controlplane spec: versions: - kubelet: 1.12.5 - controlPlane: 1.12.5 + kubelet: 1.13.3 + controlPlane: 1.13.3 providerSpec: value: apiVersion: azureprovider/v1alpha1 @@ -41,8 +41,8 @@ items: set: node spec: versions: - kubelet: 1.12.5 - controlPlane: 1.12.5 + kubelet: 1.13.3 + controlPlane: 1.13.3 providerSpec: value: apiVersion: azureprovider/v1alpha1 diff --git a/cmd/clusterctl/examples/azure/machines_no_node.yaml.template b/cmd/clusterctl/examples/azure/machines_no_node.yaml.template index 108b2e78c40..e62a95ff3bb 100644 --- a/cmd/clusterctl/examples/azure/machines_no_node.yaml.template +++ b/cmd/clusterctl/examples/azure/machines_no_node.yaml.template @@ -10,8 +10,8 @@ items: set: controlplane spec: versions: - kubelet: 1.12.5 - controlPlane: 1.12.5 + kubelet: 1.13.3 + controlPlane: 1.13.3 providerSpec: value: apiVersion: azureprovider/v1alpha1 diff --git a/config/crds/azureprovider_v1alpha1_azureclusterproviderspec.yaml b/config/crds/azureprovider_v1alpha1_azureclusterproviderspec.yaml index c983e2665c1..84ebf616f01 100644 --- a/config/crds/azureprovider_v1alpha1_azureclusterproviderspec.yaml +++ b/config/crds/azureprovider_v1alpha1_azureclusterproviderspec.yaml @@ -15,14 +15,56 @@ spec: openAPIV3Schema: properties: apiVersion: + description: 'APIVersion defines the versioned schema of this representation + of an object. Servers should convert recognized schemas to the latest + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources' type: string - caCertificate: - format: byte - type: string - caKey: - format: byte - type: string + caKeyPair: + description: CAKeyPair is the key pair for ca certs. + properties: + cert: + description: base64 encoded cert and key + format: byte + type: string + key: + format: byte + type: string + required: + - cert + - key + type: object + etcdCAKeyPair: + description: EtcdCAKeyPair is the key pair for etcd. + properties: + cert: + description: base64 encoded cert and key + format: byte + type: string + key: + format: byte + type: string + required: + - cert + - key + type: object + frontProxyCAKeyPair: + description: FrontProxyCAKeyPair is the key pair for FrontProxyKeyPair. + properties: + cert: + description: base64 encoded cert and key + format: byte + type: string + key: + format: byte + type: string + required: + - cert + - key + type: object kind: + description: 'Kind is a string value representing the REST resource this + object represents. Servers may infer this from the endpoint the client + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds' type: string location: type: string @@ -30,6 +72,20 @@ spec: type: object resourceGroup: type: string + saKeyPair: + description: SAKeyPair is the service account key pair. + properties: + cert: + description: base64 encoded cert and key + format: byte + type: string + key: + format: byte + type: string + required: + - cert + - key + type: object required: - resourceGroup - location diff --git a/config/crds/azureprovider_v1alpha1_azureclusterproviderstatus.yaml b/config/crds/azureprovider_v1alpha1_azureclusterproviderstatus.yaml index 876d3bc68dc..9f4515762f1 100644 --- a/config/crds/azureprovider_v1alpha1_azureclusterproviderstatus.yaml +++ b/config/crds/azureprovider_v1alpha1_azureclusterproviderstatus.yaml @@ -15,148 +15,179 @@ spec: openAPIV3Schema: properties: apiVersion: + description: 'APIVersion defines the versioned schema of this representation + of an object. Servers should convert recognized schemas to the latest + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources' type: string bastion: properties: - ebsOptimized: - type: boolean - enaSupport: - type: boolean - iamProfile: - type: string id: type: string - imageId: - type: string - instanceState: - type: string - keyName: + identity: type: string - privateIp: - type: string - publicIp: + image: + description: Storage profile + properties: + offer: + type: string + publisher: + type: string + sku: + type: string + version: + type: string + required: + - publisher + - offer + - sku + - version + type: object + name: type: string - securityGroupIds: - items: - type: string - type: array - subnetId: + osDisk: + properties: + diskSizeGB: + format: int32 + type: integer + managedDisk: + properties: + storageAccountType: + type: string + required: + - storageAccountType + type: object + osType: + type: string + required: + - osType + - managedDisk + - diskSizeGB + type: object + startupScript: type: string tags: type: object - type: + vmSize: + description: Hardware profile type: string - userData: + vmState: + description: State - The provisioning state, which only appears in the + response. type: string - required: - - id type: object kind: + description: 'Kind is a string value representing the REST resource this + object represents. Servers may infer this from the endpoint the client + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds' type: string metadata: type: object network: properties: apiServerIp: + description: 'APIServerIP is the Kubernetes API server public IP address. + TODO: Remove once load balancer is implemented.' properties: id: type: string ipAddress: type: string + name: + type: string required: - id + - name - ipAddress type: object apiServerLb: + description: APIServerLB is the Kubernetes API server load balancer. properties: - dnsName: - type: string - healthChecks: - properties: - healthyThreshold: - format: int64 - type: integer - interval: - format: int64 - type: integer - target: - type: string - timeout: - format: int64 - type: integer - unhealthyThreshold: - format: int64 - type: integer - required: - - target - - interval - - timeout - - healthyThreshold - - unhealthyThreshold + frontendIpConfig: type: object - ipAddress: + id: type: string - listeners: - items: - properties: - instancePort: - format: int64 - type: integer - instanceProtocol: - type: string - port: - format: int64 - type: integer - protocol: - type: string - required: - - protocol - - port - - instanceProtocol - - instancePort - type: object - type: array name: type: string - scheme: + sku: type: string - securityGroupIds: - items: - type: string - type: array - subnetIds: - items: - type: string - type: array tags: type: object type: object - internetGatewayId: - type: string securityGroups: + description: SecurityGroups is a map from the role/kind of the security + group to its unique name, if any. type: object subnets: + description: Subnets includes all the subnets defined inside the Vnet. items: properties: cidrBlock: type: string id: type: string - nsgId: - type: string - routeTableId: + name: type: string + securityGroup: + properties: + id: + type: string + ingressRule: + items: + properties: + description: + type: string + destination: + description: Destination - The destination address prefix. + CIDR or destination IP range. Asterix '*' can also + be used to match all source IPs. Default tags such + as 'VirtualNetwork', 'AzureLoadBalancer' and 'Internet' + can also be used. + type: string + destinationPorts: + description: DestinationPorts - The destination port + or range. Integer or range between 0 and 65535. Asterix + '*' can also be used to match all ports. + type: string + protocol: + type: string + source: + description: Source - The CIDR or source IP range. Asterix + '*' can also be used to match all source IPs. Default + tags such as 'VirtualNetwork', 'AzureLoadBalancer' + and 'Internet' can also be used. If this is an ingress + rule, specifies where network traffic originates from. + type: string + sourcePorts: + description: SourcePorts - The source port or range. + Integer or range between 0 and 65535. Asterix '*' + can also be used to match all ports. + type: string + required: + - description + - protocol + type: object + type: array + name: + type: string + tags: + type: object + required: + - id + - name + - ingressRule + - tags + type: object vnetId: type: string required: - - id + - name - vnetId - cidrBlock - - nsgId - - routeTableId + - securityGroup type: object type: array vnet: + description: Vnet defines the cluster vnet. properties: cidrBlock: type: string @@ -167,7 +198,6 @@ spec: tags: type: object required: - - id - cidrBlock - tags type: object diff --git a/config/crds/azureprovider_v1alpha1_azuremachineproviderspec.yaml b/config/crds/azureprovider_v1alpha1_azuremachineproviderspec.yaml index 2d329df6c09..b0033275ad2 100644 --- a/config/crds/azureprovider_v1alpha1_azuremachineproviderspec.yaml +++ b/config/crds/azureprovider_v1alpha1_azuremachineproviderspec.yaml @@ -15,6 +15,9 @@ spec: openAPIV3Schema: properties: apiVersion: + description: 'APIVersion defines the versioned schema of this representation + of an object. Servers should convert recognized schemas to the latest + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources' type: string image: properties: @@ -33,6 +36,9 @@ spec: - version type: object kind: + description: 'Kind is a string value representing the REST resource this + object represents. Servers may infer this from the endpoint the client + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds' type: string location: type: string @@ -41,7 +47,7 @@ spec: osDisk: properties: diskSizeGB: - format: int64 + format: int32 type: integer managedDisk: properties: diff --git a/config/crds/azureprovider_v1alpha1_azuremachineproviderstatus.yaml b/config/crds/azureprovider_v1alpha1_azuremachineproviderstatus.yaml index b8bdbc32d37..f51f259a71c 100644 --- a/config/crds/azureprovider_v1alpha1_azuremachineproviderstatus.yaml +++ b/config/crds/azureprovider_v1alpha1_azuremachineproviderstatus.yaml @@ -15,23 +15,37 @@ spec: openAPIV3Schema: properties: apiVersion: + description: 'APIVersion defines the versioned schema of this representation + of an object. Servers should convert recognized schemas to the latest + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources' type: string conditions: + description: Conditions is a set of conditions associated with the Machine + to indicate errors or other status. items: properties: lastProbeTime: + description: LastProbeTime is the last time we probed the condition. format: date-time type: string lastTransitionTime: + description: LastTransitionTime is the last time the condition transitioned + from one status to another. format: date-time type: string message: + description: Message is a human-readable message indicating details + about last transition. type: string reason: + description: Reason is a unique, one-word, CamelCase reason for the + condition's last transition. type: string status: + description: Status is the status of the condition. type: string type: + description: Type is the type of the condition. type: string required: - type @@ -43,12 +57,17 @@ spec: type: object type: array instanceState: + description: VMState is the state of the Azure instance for this machine. type: string kind: + description: 'Kind is a string value representing the REST resource this + object represents. Servers may infer this from the endpoint the client + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds' type: string metadata: type: object vmId: + description: VMID is the instance ID of the machine created in Azure. type: string version: v1alpha1 status: diff --git a/config/rbac/rbac_role.yaml b/config/rbac/rbac_role.yaml index 3dd28a04abd..04d2e6a049e 100644 --- a/config/rbac/rbac_role.yaml +++ b/config/rbac/rbac_role.yaml @@ -1,6 +1,7 @@ apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: + creationTimestamp: null name: manager-role rules: - apiGroups: @@ -8,8 +9,6 @@ rules: resources: - azureclusterproviderconfigs - azureclusterproviderstatuses - - azuremachineproviderconfigs - - azuremachineproviderstatuses verbs: - get - list @@ -23,6 +22,30 @@ rules: resources: - clusters - clusters/status + verbs: + - get + - list + - watch + - create + - update + - patch + - delete +- apiGroups: + - azureprovider.k8s.io + resources: + - azuremachineproviderconfigs + - azuremachineproviderstatuses + verbs: + - get + - list + - watch + - create + - update + - patch + - delete +- apiGroups: + - cluster.k8s.io + resources: - machines - machines/status - machinedeployments @@ -38,6 +61,15 @@ rules: - update - patch - delete +- apiGroups: + - cluster.k8s.io + resources: + - clusters + - clusters/status + verbs: + - get + - list + - watch - apiGroups: - "" resources: @@ -50,4 +82,4 @@ rules: - create - update - patch - - delete \ No newline at end of file + - delete diff --git a/config/rbac/rbac_role_binding.yaml b/config/rbac/rbac_role_binding.yaml index 68859350b43..c1033e23fb9 100644 --- a/config/rbac/rbac_role_binding.yaml +++ b/config/rbac/rbac_role_binding.yaml @@ -10,4 +10,4 @@ roleRef: subjects: - kind: ServiceAccount name: default - namespace: azure-provider-system + namespace: system diff --git a/docs/getting-started.md b/docs/getting-started.md index 62cf5ce3235..1f5cce5a094 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -100,13 +100,13 @@ Here's an example of how to build controller images, if you're interested in tes # Build the image. PREFIX=quay.io/k8s \ NAME=cluster-api-azure-controller \ -TAG=0.2.0-alpha.4 \ +TAG=0.1.0-alpha.3 \ make docker-build # Push the image. PREFIX=quay.io/k8s \ NAME=cluster-api-azure-controller \ -TAG=0.2.0-alpha.4 \ +TAG=0.1.0-alpha.3 \ make docker-push ``` diff --git a/pkg/apis/azureprovider/v1alpha1/azureclusterproviderconfig_types.go b/pkg/apis/azureprovider/v1alpha1/azureclusterproviderconfig_types.go index a965121d2ad..9e5f0b73db9 100644 --- a/pkg/apis/azureprovider/v1alpha1/azureclusterproviderconfig_types.go +++ b/pkg/apis/azureprovider/v1alpha1/azureclusterproviderconfig_types.go @@ -32,11 +32,24 @@ type AzureClusterProviderSpec struct { ResourceGroup string `json:"resourceGroup"` Location string `json:"location"` - // CACertificate is a PEM encoded CA Certificate for the control plane nodes. - CACertificate []byte `json:"caCertificate,omitempty"` + // CAKeyPair is the key pair for ca certs. + CAKeyPair KeyPair `json:"caKeyPair,omitempty"` - // CAPrivateKey is a PEM encoded PKCS1 CA PrivateKey for the control plane nodes. - CAPrivateKey []byte `json:"caKey,omitempty"` + //EtcdCAKeyPair is the key pair for etcd. + EtcdCAKeyPair KeyPair `json:"etcdCAKeyPair,omitempty"` + + // FrontProxyCAKeyPair is the key pair for FrontProxyKeyPair. + FrontProxyCAKeyPair KeyPair `json:"frontProxyCAKeyPair,omitempty"` + + // SAKeyPair is the service account key pair. + SAKeyPair KeyPair `json:"saKeyPair,omitempty"` +} + +// KeyPair is how operators can supply custom keypairs for kubeadm to use. +type KeyPair struct { + // base64 encoded cert and key + Cert []byte `json:"cert"` + Key []byte `json:"key"` } // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object @@ -44,3 +57,8 @@ type AzureClusterProviderSpec struct { func init() { SchemeBuilder.Register(&AzureClusterProviderSpec{}) } + +// HasCertAndKey returns whether a keypair contains cert and key of non-zero length. +func (kp *KeyPair) HasCertAndKey() bool { + return len(kp.Cert) != 0 && len(kp.Key) != 0 +} diff --git a/pkg/apis/azureprovider/v1alpha1/azureclusterproviderstatus_types.go b/pkg/apis/azureprovider/v1alpha1/azureclusterproviderstatus_types.go index e3243197a12..99733618e30 100644 --- a/pkg/apis/azureprovider/v1alpha1/azureclusterproviderstatus_types.go +++ b/pkg/apis/azureprovider/v1alpha1/azureclusterproviderstatus_types.go @@ -30,8 +30,8 @@ type AzureClusterProviderStatus struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` - Network Network `json:"network,omitempty"` - Bastion Instance `json:"bastion,omitempty"` + Network Network `json:"network,omitempty"` + Bastion VM `json:"bastion,omitempty"` } // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object diff --git a/pkg/apis/azureprovider/v1alpha1/azuremachineproviderconfig_types.go b/pkg/apis/azureprovider/v1alpha1/azuremachineproviderconfig_types.go index d06b25660ce..93ca7f994a9 100644 --- a/pkg/apis/azureprovider/v1alpha1/azuremachineproviderconfig_types.go +++ b/pkg/apis/azureprovider/v1alpha1/azuremachineproviderconfig_types.go @@ -37,29 +37,6 @@ type AzureMachineProviderSpec struct { SSHPublicKey string `json:"sshPublicKey"` SSHPrivateKey string `json:"sshPrivateKey"` } -type MachineRole string - -const ( - Master MachineRole = "Master" - Node MachineRole = "Node" -) - -type Image struct { - Publisher string `json:"publisher"` - Offer string `json:"offer"` - SKU string `json:"sku"` - Version string `json:"version"` -} - -type OSDisk struct { - OSType string `json:"osType"` - ManagedDisk ManagedDisk `json:"managedDisk"` - DiskSizeGB int `json:"diskSizeGB"` -} - -type ManagedDisk struct { - StorageAccountType string `json:"storageAccountType"` -} // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object diff --git a/pkg/apis/azureprovider/v1alpha1/azuremachineproviderstatus_types.go b/pkg/apis/azureprovider/v1alpha1/azuremachineproviderstatus_types.go index c18cbb89f1f..80534cf4aa3 100644 --- a/pkg/apis/azureprovider/v1alpha1/azuremachineproviderstatus_types.go +++ b/pkg/apis/azureprovider/v1alpha1/azuremachineproviderstatus_types.go @@ -33,9 +33,9 @@ type AzureMachineProviderStatus struct { // +optional VMID *string `json:"vmId,omitempty"` - // InstanceState is the state of the Azure instance for this machine. + // VMState is the state of the Azure instance for this machine. // +optional - InstanceState *string `json:"instanceState,omitempty"` + VMState *string `json:"instanceState,omitempty"` // Conditions is a set of conditions associated with the Machine to indicate // errors or other status. diff --git a/pkg/apis/azureprovider/v1alpha1/types.go b/pkg/apis/azureprovider/v1alpha1/types.go index 33ff478f23e..b0fc4372385 100644 --- a/pkg/apis/azureprovider/v1alpha1/types.go +++ b/pkg/apis/azureprovider/v1alpha1/types.go @@ -17,9 +17,6 @@ limitations under the License. package v1alpha1 import ( - "fmt" - "reflect" - "sort" "time" corev1 "k8s.io/api/core/v1" @@ -66,15 +63,19 @@ type AzureMachineProviderCondition struct { Message string `json:"message"` } -// TODO +type MachineRole string + +const ( + // TODO: Change references to "controlplane", instead of "Master" and lowercase both roles. + Master MachineRole = "Master" + Node MachineRole = "Node" +) + // Network encapsulates Azure networking resources. type Network struct { // Vnet defines the cluster vnet. Vnet Vnet `json:"vnet,omitempty"` - // InternetGatewayID is the id of the internet gateway associated with the Vnet. - InternetGatewayID *string `json:"internetGatewayId,omitempty"` - // SecurityGroups is a map from the role/kind of the security group to its unique name, if any. SecurityGroups map[SecurityGroupRole]*SecurityGroup `json:"securityGroups,omitempty"` @@ -85,133 +86,21 @@ type Network struct { APIServerLB LoadBalancer `json:"apiServerLb,omitempty"` // APIServerIP is the Kubernetes API server public IP address. - APIServerIP PublicIPAddress `json:"apiServerIp,omitempty"` + // TODO: Remove once load balancer is implemented. + APIServerIP PublicIP `json:"apiServerIp,omitempty"` } +// Tags defines resource tags. +type Tags map[string]*string + // Vnet defines an Azure Virtual Network. type Vnet struct { - ID string `json:"id"` - CidrBlock string `json:"cidrBlock"` - Name *string `json:"name,omitempty"` - Tags map[string]*string `json:"tags"` -} - -// TODO: Do we need this? -// String returns a string representation of the Vnet. -func (v *Vnet) String() string { - return fmt.Sprintf("id=%s", v.ID) -} - -// Subnet defines an Azure subnet attached to a Vnet. -type Subnet struct { - ID string `json:"id"` - - VnetID string `json:"vnetId"` - CidrBlock string `json:"cidrBlock"` - NSGID string `json:"nsgId"` - RouteTableID *string `json:"routeTableId"` -} - -// PublicIPAddress defines an Azure public IP address. -type PublicIPAddress struct { - ID string `json:"id"` - IPAddress string `json:"ipAddress"` -} - -/* -// TODO -// String returns a string representation of the subnet. -func (s *Subnet) String() string { - return fmt.Sprintf("id=%s/az=%s/public=%v", s.ID, s.AvailabilityZone, s.IsPublic) -} -*/ - -// TODO -// LoadBalancerScheme defines the scheme of a load balancer. -type LoadBalancerScheme string - -// TODO -var ( - // LoadBalancerSchemeInternetFacing defines an internet-facing, publicly - // accessible Azure LB scheme - LoadBalancerSchemeInternetFacing = LoadBalancerScheme("Internet-facing") - - // LoadBalancerSchemeInternal defines an internal-only facing - // load balancer internal to a LB. - LoadBalancerSchemeInternal = LoadBalancerScheme("internal") -) - -// TODO -// LoadBalancerProtocol defines listener protocols for a load balancer. -type LoadBalancerProtocol string - -// TODO -var ( - // LoadBalancerProtocolTCP defines the LB API string representing the TCP protocol - LoadBalancerProtocolTCP = LoadBalancerProtocol("TCP") - - // LoadBalancerProtocolSSL defines the LB API string representing the TLS protocol - LoadBalancerProtocolSSL = LoadBalancerProtocol("SSL") - - // LoadBalancerProtocolHTTP defines the LB API string representing the HTTP protocol at L7 - LoadBalancerProtocolHTTP = LoadBalancerProtocol("HTTP") - - // LoadBalancerProtocolHTTPS defines the LB API string representing the HTTP protocol at L7 - LoadBalancerProtocolHTTPS = LoadBalancerProtocol("HTTPS") -) - -// TODO -// LoadBalancer defines an Azure load balancer. -type LoadBalancer struct { - // The name of the load balancer. It must be unique within the set of load balancers - // defined in the location. It also serves as identifier. - Name string `json:"name,omitempty"` - - // DNSName is the dns name of the load balancer. - DNSName string `json:"dnsName,omitempty"` - - // IPAddress is the IP address of the load balancer. - IPAddress string `json:"ipAddress,omitempty"` - - // Scheme is the load balancer scheme, either internet-facing or private. - Scheme LoadBalancerScheme `json:"scheme,omitempty"` - - // SubnetIDs is an array of subnets in the Vnet attached to the load balancer. - SubnetIDs []string `json:"subnetIds,omitempty"` - - // SecurityGroupIDs is an array of security groups assigned to the load balancer. - SecurityGroupIDs []string `json:"securityGroupIds,omitempty"` - - // Listeners is an array of elb listeners associated with the load balancer. There must be at least one. - Listeners []*LoadBalancerListener `json:"listeners,omitempty"` - - // HealthCheck is the elb health check associated with the load balancer. - HealthCheck *LoadBalancerHealthCheck `json:"healthChecks,omitempty"` - - // Tags is a map of tags associated with the load balancer. - Tags map[string]string `json:"tags,omitempty"` -} - -// TODO -// LoadBalancerListener defines an Azure load balancer listener. -type LoadBalancerListener struct { - Protocol LoadBalancerProtocol `json:"protocol"` - Port int64 `json:"port"` - InstanceProtocol LoadBalancerProtocol `json:"instanceProtocol"` - InstancePort int64 `json:"instancePort"` + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` + CidrBlock string `json:"cidrBlock"` + Tags *Tags `json:"tags"` } -// TODO -// LoadBalancerHealthCheck defines an Azure load balancer health check. -type LoadBalancerHealthCheck struct { - Target string `json:"target"` - Interval time.Duration `json:"interval"` - Timeout time.Duration `json:"timeout"` - HealthyThreshold int64 `json:"healthyThreshold"` - UnhealthyThreshold int64 `json:"unhealthyThreshold"` -} - -// TODO // Subnets is a slice of Subnet. type Subnets []*Subnet @@ -225,9 +114,13 @@ func (s Subnets) ToMap() map[string]*Subnet { return res } -// RouteTable defines an Azure routing table. -type RouteTable struct { - ID string `json:"id"` +// Subnet defines an Azure subnet attached to a Vnet. +type Subnet struct { + ID string `json:"id,omitempty"` + Name string `json:"name"` + VnetID string `json:"vnetId"` + CidrBlock string `json:"cidrBlock"` + SecurityGroup SecurityGroup `json:"securityGroup"` } // SecurityGroupRole defines the unique role of a security group. @@ -244,13 +137,12 @@ var ( SecurityGroupControlPlane = SecurityGroupRole("controlplane") ) -// TODO // SecurityGroup defines an Azure security group. type SecurityGroup struct { - ID string `json:"id"` - Name string `json:"name"` - + ID string `json:"id"` + Name string `json:"name"` IngressRules IngressRules `json:"ingressRule"` + Tags *Tags `json:"tags"` } /* @@ -261,20 +153,18 @@ func (s *SecurityGroup) String() string { } */ -// TODO // SecurityGroupProtocol defines the protocol type for a security group rule. type SecurityGroupProtocol string -// TODO var ( // SecurityGroupProtocolAll is a wildcard for all IP protocols SecurityGroupProtocolAll = SecurityGroupProtocol("*") // SecurityGroupProtocolTCP represents the TCP protocol in ingress rules - SecurityGroupProtocolTCP = SecurityGroupProtocol("tcp") + SecurityGroupProtocolTCP = SecurityGroupProtocol("Tcp") // SecurityGroupProtocolUDP represents the UDP protocol in ingress rules - SecurityGroupProtocolUDP = SecurityGroupProtocol("udp") + SecurityGroupProtocolUDP = SecurityGroupProtocol("Udp") ) // TODO @@ -282,21 +172,27 @@ var ( type IngressRule struct { Description string `json:"description"` Protocol SecurityGroupProtocol `json:"protocol"` - FromPort int64 `json:"fromPort"` - ToPort int64 `json:"toPort"` - // List of CIDR blocks to allow access from. Cannot be specified with SourceSecurityGroupID. - CidrBlocks []string `json:"cidrBlocks"` + // SourcePorts - The source port or range. Integer or range between 0 and 65535. Asterix '*' can also be used to match all ports. + SourcePorts *string `json:"sourcePorts,omitempty"` - // The security group id to allow access from. Cannot be specified with CidrBlocks. - SourceSecurityGroupIDs []string `json:"sourceSecurityGroupIds"` + // DestinationPorts - The destination port or range. Integer or range between 0 and 65535. Asterix '*' can also be used to match all ports. + DestinationPorts *string `json:"destinationPorts,omitempty"` + + // Source - The CIDR or source IP range. Asterix '*' can also be used to match all source IPs. Default tags such as 'VirtualNetwork', 'AzureLoadBalancer' and 'Internet' can also be used. If this is an ingress rule, specifies where network traffic originates from. + Source *string `json:"source,omitempty"` + + // Destination - The destination address prefix. CIDR or destination IP range. Asterix '*' can also be used to match all source IPs. Default tags such as 'VirtualNetwork', 'AzureLoadBalancer' and 'Internet' can also be used. + Destination *string `json:"destination,omitempty"` } // TODO // String returns a string representation of the ingress rule. +/* func (i *IngressRule) String() string { return fmt.Sprintf("protocol=%s/range=[%d-%d]/description=%s", i.Protocol, i.FromPort, i.ToPort, i.Description) } +*/ // TODO // IngressRules is a slice of Azure ingress rules for security groups. @@ -304,6 +200,7 @@ type IngressRules []*IngressRule // TODO // Difference returns the difference between this slice and the other slice. +/* func (i IngressRules) Difference(o IngressRules) (out IngressRules) { for _, x := range i { found := false @@ -325,76 +222,178 @@ func (i IngressRules) Difference(o IngressRules) (out IngressRules) { return } +*/ -// TODO -// InstanceState describes the state of an Azure instance. -type InstanceState string +// PublicIP defines an Azure public IP address. +// TODO: Remove once load balancer is implemented. +type PublicIP struct { + ID string `json:"id"` + Name string `json:"name"` + IPAddress string `json:"ipAddress"` +} // TODO +// LoadBalancer defines an Azure load balancer. +type LoadBalancer struct { + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` + SKU SKU `json:"sku,omitempty"` + FrontendIPConfig FrontendIPConfig `json:"frontendIpConfig,omitempty"` + Tags Tags `json:"tags,omitempty"` + /* + // FrontendIPConfigurations - Object representing the frontend IPs to be used for the load balancer + FrontendIPConfigurations *[]FrontendIPConfiguration `json:"frontendIPConfigurations,omitempty"` + // BackendAddressPools - Collection of backend address pools used by a load balancer + BackendAddressPools *[]BackendAddressPool `json:"backendAddressPools,omitempty"` + // LoadBalancingRules - Object collection representing the load balancing rules Gets the provisioning + LoadBalancingRules *[]LoadBalancingRule `json:"loadBalancingRules,omitempty"` + // Probes - Collection of probe objects used in the load balancer + Probes *[]Probe `json:"probes,omitempty"` + // InboundNatRules - Collection of inbound NAT Rules used by a load balancer. Defining inbound NAT rules on your load balancer is mutually exclusive with defining an inbound NAT pool. Inbound NAT pools are referenced from virtual machine scale sets. NICs that are associated with individual virtual machines cannot reference an Inbound NAT pool. They have to reference individual inbound NAT rules. + InboundNatRules *[]InboundNatRule `json:"inboundNatRules,omitempty"` + // InboundNatPools - Defines an external port range for inbound NAT to a single backend port on NICs associated with a load balancer. Inbound NAT rules are created automatically for each NIC associated with the Load Balancer using an external port from this range. Defining an Inbound NAT pool on your Load Balancer is mutually exclusive with defining inbound Nat rules. Inbound NAT pools are referenced from virtual machine scale sets. NICs that are associated with individual virtual machines cannot reference an inbound NAT pool. They have to reference individual inbound NAT rules. + InboundNatPools *[]InboundNatPool `json:"inboundNatPools,omitempty"` + // OutboundRules - The outbound rules. + OutboundRules *[]OutboundRule `json:"outboundRules,omitempty"` + // ResourceGUID - The resource GUID property of the load balancer resource. + ResourceGUID *string `json:"resourceGuid,omitempty"` + // ProvisioningState - Gets the provisioning state of the PublicIP resource. Possible values are: 'Updating', 'Deleting', and 'Failed'. + ProvisioningState *string `json:"provisioningState,omitempty"` + */ +} + +// LoadBalancerSKU enumerates the values for load balancer sku name. +type SKU string + var ( - // InstanceStatePending is the string representing an instance in a pending state - InstanceStatePending = InstanceState("pending") + SKUBasic = SKU("Basic") + SKUStandard = SKU("Standard") +) - // InstanceStateRunning is the string representing an instance in a pending state - InstanceStateRunning = InstanceState("running") +type FrontendIPConfig struct { + /* + // FrontendIPConfigurationPropertiesFormat - Properties of the load balancer probe. + *FrontendIPConfigurationPropertiesFormat `json:"properties,omitempty"` + // Name - The name of the resource that is unique within a resource group. This name can be used to access the resource. + Name *string `json:"name,omitempty"` + // Etag - A unique read-only string that changes whenever the resource is updated. + Etag *string `json:"etag,omitempty"` + // Zones - A list of availability zones denoting the IP allocated for the resource needs to come from. + Zones *[]string `json:"zones,omitempty"` + // ID - Resource ID. + ID *string `json:"id,omitempty"` + */ +} - // InstanceStateShuttingDown is the string representing an instance shutting down - InstanceStateShuttingDown = InstanceState("shutting-down") +// TODO +// LoadBalancerProtocol defines listener protocols for a load balancer. +type LoadBalancerProtocol string - // InstanceStateTerminated is the string representing an instance that has been terminated - InstanceStateTerminated = InstanceState("terminated") +// TODO +var ( + // LoadBalancerProtocolTCP defines the LB API string representing the TCP protocol + LoadBalancerProtocolTCP = LoadBalancerProtocol("TCP") - // InstanceStateStopping is the string representing an instance - // that is in the process of being stopped and can be restarted - InstanceStateStopping = InstanceState("stopping") + // LoadBalancerProtocolSSL defines the LB API string representing the TLS protocol + LoadBalancerProtocolSSL = LoadBalancerProtocol("SSL") - // InstanceStateStopped is the string representing an instance - // that has been stopped and can be restarted - InstanceStateStopped = InstanceState("stopped") + // LoadBalancerProtocolHTTP defines the LB API string representing the HTTP protocol at L7 + LoadBalancerProtocolHTTP = LoadBalancerProtocol("HTTP") + + // LoadBalancerProtocolHTTPS defines the LB API string representing the HTTP protocol at L7 + LoadBalancerProtocolHTTPS = LoadBalancerProtocol("HTTPS") ) // TODO -// Instance describes an Azure instance. -type Instance struct { - ID string `json:"id"` +// LoadBalancerListener defines an Azure load balancer listener. +type LoadBalancerListener struct { + Protocol LoadBalancerProtocol `json:"protocol"` + Port int64 `json:"port"` + InstanceProtocol LoadBalancerProtocol `json:"instanceProtocol"` + InstancePort int64 `json:"instancePort"` +} + +// TODO +// LoadBalancerHealthCheck defines an Azure load balancer health check. +type LoadBalancerHealthCheck struct { + Target string `json:"target"` + Interval time.Duration `json:"interval"` + Timeout time.Duration `json:"timeout"` + HealthyThreshold int64 `json:"healthyThreshold"` + UnhealthyThreshold int64 `json:"unhealthyThreshold"` +} + +// VMState describes the state of an Azure virtual machine. +type VMState string - // The current state of the instance. - State InstanceState `json:"instanceState,omitempty"` +var ( + // VMStateCreating ... + VMStateCreating = VMState("Creating") + // VMStateDeleting ... + VMStateDeleting = VMState("Deleting") + // VMStateFailed ... + VMStateFailed = VMState("Failed") + // VMStateMigrating ... + VMStateMigrating = VMState("Migrating") + // VMStateSucceeded ... + VMStateSucceeded = VMState("Succeeded") + // VMStateUpdating ... + VMStateUpdating = VMState("Updating") +) - // The instance type. - Type string `json:"type,omitempty"` +// VM describes an Azure virtual machine. +type VM struct { + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` - // The ID of the subnet of the instance. - SubnetID string `json:"subnetId,omitempty"` + // Hardware profile + VMSize string `json:"vmSize,omitempty"` - // The ID of the AMI used to launch the instance. - ImageID string `json:"imageId,omitempty"` + // Storage profile + Image Image `json:"image,omitempty"` + OSDisk OSDisk `json:"osDisk,omitempty"` - // The name of the SSH key pair. - KeyName *string `json:"keyName,omitempty"` + StartupScript string `json:"startupScript,omitempty"` - // SecurityGroupIDs are one or more security group IDs this instance belongs to. - SecurityGroupIDs []string `json:"securityGroupIds,omitempty"` + // State - The provisioning state, which only appears in the response. + State VMState `json:"vmState,omitempty"` + Identity VMIdentity `json:"identity,omitempty"` - // UserData is the raw data script passed to the instance which is run upon bootstrap. - // This field must not be base64 encoded and should only be used when running a new instance. - UserData *string `json:"userData,omitempty"` + Tags Tags `json:"tags,omitempty"` - // The name of the IAM instance profile associated with the instance, if applicable. - IAMProfile string `json:"iamProfile,omitempty"` + // HardwareProfile - Specifies the hardware settings for the virtual machine. + //HardwareProfile *HardwareProfile `json:"hardwareProfile,omitempty"` - // The private IPv4 address assigned to the instance. - PrivateIP *string `json:"privateIp,omitempty"` + // StorageProfile - Specifies the storage settings for the virtual machine disks. + //StorageProfile *StorageProfile `json:"storageProfile,omitempty"` - // The public IPv4 address assigned to the instance, if applicable. - PublicIP *string `json:"publicIp,omitempty"` + // AdditionalCapabilities - Specifies additional capabilities enabled or disabled on the virtual machine. + //AdditionalCapabilities *AdditionalCapabilities `json:"additionalCapabilities,omitempty"` - // Specifies whether enhanced networking with ENA is enabled. - ENASupport *bool `json:"enaSupport,omitempty"` + // OsProfile - Specifies the operating system settings for the virtual machine. + //OsProfile *OSProfile `json:"osProfile,omitempty"` + // NetworkProfile - Specifies the network interfaces of the virtual machine. + //NetworkProfile *NetworkProfile `json:"networkProfile,omitempty"` + + //AvailabilitySet *SubResource `json:"availabilitySet,omitempty"` +} - // Indicates whether the instance is optimized for Amazon EBS I/O. - EBSOptimized *bool `json:"ebsOptimized,omitempty"` +type Image struct { + Publisher string `json:"publisher"` + Offer string `json:"offer"` + SKU string `json:"sku"` + Version string `json:"version"` +} + +// VMIdentity defines the identity of the virtual machine, if configured. +type VMIdentity string + +type OSDisk struct { + OSType string `json:"osType"` + ManagedDisk ManagedDisk `json:"managedDisk"` + DiskSizeGB int32 `json:"diskSizeGB"` +} - // The tags associated with the instance. - Tags map[string]string `json:"tags,omitempty"` +type ManagedDisk struct { + StorageAccountType string `json:"storageAccountType"` } diff --git a/pkg/apis/azureprovider/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/azureprovider/v1alpha1/zz_generated.deepcopy.go index 4352b9ebc7a..8f226d56faa 100644 --- a/pkg/apis/azureprovider/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/azureprovider/v1alpha1/zz_generated.deepcopy.go @@ -29,16 +29,10 @@ func (in *AzureClusterProviderSpec) DeepCopyInto(out *AzureClusterProviderSpec) *out = *in out.TypeMeta = in.TypeMeta in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - if in.CACertificate != nil { - in, out := &in.CACertificate, &out.CACertificate - *out = make([]byte, len(*in)) - copy(*out, *in) - } - if in.CAPrivateKey != nil { - in, out := &in.CAPrivateKey, &out.CAPrivateKey - *out = make([]byte, len(*in)) - copy(*out, *in) - } + in.CAKeyPair.DeepCopyInto(&out.CAKeyPair) + in.EtcdCAKeyPair.DeepCopyInto(&out.EtcdCAKeyPair) + in.FrontProxyCAKeyPair.DeepCopyInto(&out.FrontProxyCAKeyPair) + in.SAKeyPair.DeepCopyInto(&out.SAKeyPair) return } @@ -149,8 +143,8 @@ func (in *AzureMachineProviderStatus) DeepCopyInto(out *AzureMachineProviderStat *out = new(string) **out = **in } - if in.InstanceState != nil { - in, out := &in.InstanceState, &out.InstanceState + if in.VMState != nil { + in, out := &in.VMState, &out.VMState *out = new(string) **out = **in } @@ -203,6 +197,22 @@ func (in *AzureResourceReference) DeepCopy() *AzureResourceReference { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *FrontendIPConfig) DeepCopyInto(out *FrontendIPConfig) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new FrontendIPConfig. +func (in *FrontendIPConfig) DeepCopy() *FrontendIPConfig { + if in == nil { + return nil + } + out := new(FrontendIPConfig) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Image) DeepCopyInto(out *Image) { *out = *in @@ -222,15 +232,25 @@ func (in *Image) DeepCopy() *Image { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *IngressRule) DeepCopyInto(out *IngressRule) { *out = *in - if in.CidrBlocks != nil { - in, out := &in.CidrBlocks, &out.CidrBlocks - *out = make([]string, len(*in)) - copy(*out, *in) + if in.SourcePorts != nil { + in, out := &in.SourcePorts, &out.SourcePorts + *out = new(string) + **out = **in } - if in.SourceSecurityGroupIDs != nil { - in, out := &in.SourceSecurityGroupIDs, &out.SourceSecurityGroupIDs - *out = make([]string, len(*in)) - copy(*out, *in) + if in.DestinationPorts != nil { + in, out := &in.DestinationPorts, &out.DestinationPorts + *out = new(string) + **out = **in + } + if in.Source != nil { + in, out := &in.Source, &out.Source + *out = new(string) + **out = **in + } + if in.Destination != nil { + in, out := &in.Destination, &out.Destination + *out = new(string) + **out = **in } return } @@ -272,59 +292,27 @@ func (in IngressRules) DeepCopy() IngressRules { } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *Instance) DeepCopyInto(out *Instance) { +func (in *KeyPair) DeepCopyInto(out *KeyPair) { *out = *in - if in.KeyName != nil { - in, out := &in.KeyName, &out.KeyName - *out = new(string) - **out = **in - } - if in.SecurityGroupIDs != nil { - in, out := &in.SecurityGroupIDs, &out.SecurityGroupIDs - *out = make([]string, len(*in)) + if in.Cert != nil { + in, out := &in.Cert, &out.Cert + *out = make([]byte, len(*in)) copy(*out, *in) } - if in.UserData != nil { - in, out := &in.UserData, &out.UserData - *out = new(string) - **out = **in - } - if in.PrivateIP != nil { - in, out := &in.PrivateIP, &out.PrivateIP - *out = new(string) - **out = **in - } - if in.PublicIP != nil { - in, out := &in.PublicIP, &out.PublicIP - *out = new(string) - **out = **in - } - if in.ENASupport != nil { - in, out := &in.ENASupport, &out.ENASupport - *out = new(bool) - **out = **in - } - if in.EBSOptimized != nil { - in, out := &in.EBSOptimized, &out.EBSOptimized - *out = new(bool) - **out = **in - } - if in.Tags != nil { - in, out := &in.Tags, &out.Tags - *out = make(map[string]string, len(*in)) - for key, val := range *in { - (*out)[key] = val - } + if in.Key != nil { + in, out := &in.Key, &out.Key + *out = make([]byte, len(*in)) + copy(*out, *in) } return } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Instance. -func (in *Instance) DeepCopy() *Instance { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KeyPair. +func (in *KeyPair) DeepCopy() *KeyPair { if in == nil { return nil } - out := new(Instance) + out := new(KeyPair) in.DeepCopyInto(out) return out } @@ -332,37 +320,20 @@ func (in *Instance) DeepCopy() *Instance { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *LoadBalancer) DeepCopyInto(out *LoadBalancer) { *out = *in - if in.SubnetIDs != nil { - in, out := &in.SubnetIDs, &out.SubnetIDs - *out = make([]string, len(*in)) - copy(*out, *in) - } - if in.SecurityGroupIDs != nil { - in, out := &in.SecurityGroupIDs, &out.SecurityGroupIDs - *out = make([]string, len(*in)) - copy(*out, *in) - } - if in.Listeners != nil { - in, out := &in.Listeners, &out.Listeners - *out = make([]*LoadBalancerListener, len(*in)) - for i := range *in { - if (*in)[i] != nil { - in, out := &(*in)[i], &(*out)[i] - *out = new(LoadBalancerListener) - **out = **in - } - } - } - if in.HealthCheck != nil { - in, out := &in.HealthCheck, &out.HealthCheck - *out = new(LoadBalancerHealthCheck) - **out = **in - } + out.FrontendIPConfig = in.FrontendIPConfig if in.Tags != nil { in, out := &in.Tags, &out.Tags - *out = make(map[string]string, len(*in)) + *out = make(Tags, len(*in)) for key, val := range *in { - (*out)[key] = val + var outVal *string + if val == nil { + (*out)[key] = nil + } else { + in, out := &val, &outVal + *out = new(string) + **out = **in + } + (*out)[key] = outVal } } return @@ -430,11 +401,6 @@ func (in *ManagedDisk) DeepCopy() *ManagedDisk { func (in *Network) DeepCopyInto(out *Network) { *out = *in in.Vnet.DeepCopyInto(&out.Vnet) - if in.InternetGatewayID != nil { - in, out := &in.InternetGatewayID, &out.InternetGatewayID - *out = new(string) - **out = **in - } if in.SecurityGroups != nil { in, out := &in.SecurityGroups, &out.SecurityGroups *out = make(map[SecurityGroupRole]*SecurityGroup, len(*in)) @@ -494,33 +460,17 @@ func (in *OSDisk) DeepCopy() *OSDisk { } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *PublicIPAddress) DeepCopyInto(out *PublicIPAddress) { +func (in *PublicIP) DeepCopyInto(out *PublicIP) { *out = *in return } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new PublicIPAddress. -func (in *PublicIPAddress) DeepCopy() *PublicIPAddress { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new PublicIP. +func (in *PublicIP) DeepCopy() *PublicIP { if in == nil { return nil } - out := new(PublicIPAddress) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *RouteTable) DeepCopyInto(out *RouteTable) { - *out = *in - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RouteTable. -func (in *RouteTable) DeepCopy() *RouteTable { - if in == nil { - return nil - } - out := new(RouteTable) + out := new(PublicIP) in.DeepCopyInto(out) return out } @@ -539,6 +489,25 @@ func (in *SecurityGroup) DeepCopyInto(out *SecurityGroup) { } } } + if in.Tags != nil { + in, out := &in.Tags, &out.Tags + *out = new(Tags) + if **in != nil { + in, out := *in, *out + *out = make(map[string]*string, len(*in)) + for key, val := range *in { + var outVal *string + if val == nil { + (*out)[key] = nil + } else { + in, out := &val, &outVal + *out = new(string) + **out = **in + } + (*out)[key] = outVal + } + } + } return } @@ -555,11 +524,7 @@ func (in *SecurityGroup) DeepCopy() *SecurityGroup { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Subnet) DeepCopyInto(out *Subnet) { *out = *in - if in.RouteTableID != nil { - in, out := &in.RouteTableID, &out.RouteTableID - *out = new(string) - **out = **in - } + in.SecurityGroup.DeepCopyInto(&out.SecurityGroup) return } @@ -600,16 +565,43 @@ func (in Subnets) DeepCopy() Subnets { } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *Vnet) DeepCopyInto(out *Vnet) { - *out = *in - if in.Name != nil { - in, out := &in.Name, &out.Name - *out = new(string) - **out = **in +func (in Tags) DeepCopyInto(out *Tags) { + { + in := &in + *out = make(Tags, len(*in)) + for key, val := range *in { + var outVal *string + if val == nil { + (*out)[key] = nil + } else { + in, out := &val, &outVal + *out = new(string) + **out = **in + } + (*out)[key] = outVal + } + return + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Tags. +func (in Tags) DeepCopy() Tags { + if in == nil { + return nil } + out := new(Tags) + in.DeepCopyInto(out) + return *out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *VM) DeepCopyInto(out *VM) { + *out = *in + out.Image = in.Image + out.OSDisk = in.OSDisk if in.Tags != nil { in, out := &in.Tags, &out.Tags - *out = make(map[string]*string, len(*in)) + *out = make(Tags, len(*in)) for key, val := range *in { var outVal *string if val == nil { @@ -625,6 +617,41 @@ func (in *Vnet) DeepCopyInto(out *Vnet) { return } +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new VM. +func (in *VM) DeepCopy() *VM { + if in == nil { + return nil + } + out := new(VM) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Vnet) DeepCopyInto(out *Vnet) { + *out = *in + if in.Tags != nil { + in, out := &in.Tags, &out.Tags + *out = new(Tags) + if **in != nil { + in, out := *in, *out + *out = make(map[string]*string, len(*in)) + for key, val := range *in { + var outVal *string + if val == nil { + (*out)[key] = nil + } else { + in, out := &val, &outVal + *out = new(string) + **out = **in + } + (*out)[key] = outVal + } + } + } + return +} + // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Vnet. func (in *Vnet) DeepCopy() *Vnet { if in == nil { diff --git a/pkg/cloud/azure/actuators/BUILD.bazel b/pkg/cloud/azure/actuators/BUILD.bazel index 62c45335fbf..a8da528ebbd 100644 --- a/pkg/cloud/azure/actuators/BUILD.bazel +++ b/pkg/cloud/azure/actuators/BUILD.bazel @@ -1,10 +1,11 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "go_default_library", srcs = [ "clients.go", "getters.go", + "helpers.go", "machine_scope.go", "scope.go", ], @@ -12,6 +13,7 @@ go_library( visibility = ["//visibility:public"], deps = [ "//pkg/apis/azureprovider/v1alpha1:go_default_library", + "//pkg/cloud/azure/services/certificates:go_default_library", "//vendor/github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-10-01/compute:go_default_library", "//vendor/github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-11-01/network:go_default_library", "//vendor/github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources:go_default_library", @@ -26,3 +28,14 @@ go_library( "//vendor/sigs.k8s.io/yaml:go_default_library", ], ) + +go_test( + name = "go_default_test", + srcs = ["helpers_test.go"], + embed = [":go_default_library"], + deps = [ + "//pkg/apis/azureprovider/v1alpha1:go_default_library", + "//pkg/cloud/azure/services/certificates:go_default_library", + "//vendor/github.com/pkg/errors:go_default_library", + ], +) diff --git a/pkg/cloud/azure/actuators/clients.go b/pkg/cloud/azure/actuators/clients.go index 4589ad31959..bd7d83ca133 100644 --- a/pkg/cloud/azure/actuators/clients.go +++ b/pkg/cloud/azure/actuators/clients.go @@ -75,7 +75,7 @@ type AzureNetworkClient interface { WaitForNetworkSGsCreateOrUpdateFuture(future network.SecurityGroupsCreateOrUpdateFuture) error // Public Ip Address Operations - GetPublicIPAddress(resourceGroupName string, IPName string) (network.PublicIPAddress, error) + CreateOrUpdatePublicIPAddress(resourceGroupName string, IPName string) (network.PublicIPAddress, error) DeletePublicIPAddress(resourceGroup string, IPName string) (network.PublicIPAddressesDeleteFuture, error) WaitForPublicIPAddressDeleteFuture(future network.PublicIPAddressesDeleteFuture) error @@ -93,8 +93,8 @@ type AzureResourcesClient interface { WaitForGroupsDeleteFuture(future resources.GroupsDeleteFuture) error // Deployment Operations - CreateOrUpdateDeployment(machine *clusterv1.Machine, clusterConfig *providerv1.AzureClusterProviderSpec, machineConfig *providerv1.AzureMachineProviderSpec) (*resources.DeploymentsCreateOrUpdateFuture, error) + CreateOrUpdateDeployment(machine *clusterv1.Machine, clusterConfig *providerv1.AzureClusterProviderSpec, machineConfig *providerv1.AzureMachineProviderSpec, startupScript string) (*resources.DeploymentsCreateOrUpdateFuture, error) GetDeploymentResult(future resources.DeploymentsCreateOrUpdateFuture) (de resources.DeploymentExtended, err error) - ValidateDeployment(machine *clusterv1.Machine, clusterConfig *providerv1.AzureClusterProviderSpec, machineConfig *providerv1.AzureMachineProviderSpec) error + ValidateDeployment(machine *clusterv1.Machine, clusterConfig *providerv1.AzureClusterProviderSpec, machineConfig *providerv1.AzureMachineProviderSpec, startupScript string) error WaitForDeploymentsCreateOrUpdateFuture(future resources.DeploymentsCreateOrUpdateFuture) error } diff --git a/pkg/cloud/azure/actuators/cluster/BUILD.bazel b/pkg/cloud/azure/actuators/cluster/BUILD.bazel index d5864d78e5b..482d56b8edc 100644 --- a/pkg/cloud/azure/actuators/cluster/BUILD.bazel +++ b/pkg/cloud/azure/actuators/cluster/BUILD.bazel @@ -6,6 +6,7 @@ go_library( importpath = "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/actuators/cluster", visibility = ["//visibility:public"], deps = [ + "//pkg/apis/azureprovider/v1alpha1:go_default_library", "//pkg/cloud/azure/actuators:go_default_library", "//pkg/cloud/azure/services/network:go_default_library", "//pkg/cloud/azure/services/resources:go_default_library", diff --git a/pkg/cloud/azure/actuators/cluster/actuator.go b/pkg/cloud/azure/actuators/cluster/actuator.go index 48f6e96d287..96c2d958a09 100644 --- a/pkg/cloud/azure/actuators/cluster/actuator.go +++ b/pkg/cloud/azure/actuators/cluster/actuator.go @@ -17,10 +17,9 @@ limitations under the License. package cluster import ( - "fmt" - "github.com/pkg/errors" "k8s.io/klog" + "sigs.k8s.io/cluster-api-provider-azure/pkg/apis/azureprovider/v1alpha1" "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/actuators" "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/network" "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/resources" @@ -29,6 +28,9 @@ import ( client "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1" ) +//+kubebuilder:rbac:groups=azureprovider.k8s.io,resources=azureclusterproviderconfigs;azureclusterproviderstatuses,verbs=get;list;watch;create;update;patch;delete +//+kubebuilder:rbac:groups=cluster.k8s.io,resources=clusters;clusters/status,verbs=get;list;watch;create;update;patch;delete + // Actuator is responsible for performing cluster reconciliation type Actuator struct { *deployer.Deployer @@ -63,31 +65,70 @@ func (a *Actuator) Reconcile(cluster *clusterv1.Cluster) error { networkSvc := network.NewService(scope) resourcesSvc := resources.NewService(scope) - // Reconcile resource group - _, err = resourcesSvc.CreateOrUpdateGroup(scope.ClusterConfig.ResourceGroup, scope.ClusterConfig.Location) - if err != nil { - return fmt.Errorf("failed to create or update resource group: %v", err) + // Store some config parameters in the status. + if !scope.ClusterConfig.CAKeyPair.HasCertAndKey() { + caCert, caKey, err := actuators.GetOrGenerateKeyPair(&scope.ClusterConfig.CAKeyPair, actuators.ClusterCA) + if err != nil { + return errors.Wrap(err, "Failed to generate a CA for the control plane") + } + scope.ClusterConfig.CAKeyPair = v1alpha1.KeyPair{ + Cert: caCert, + Key: caKey, + } } - // Reconcile network security group - networkSGFuture, err := networkSvc.CreateOrUpdateNetworkSecurityGroup(scope.ClusterConfig.ResourceGroup, "ClusterAPINSG", scope.ClusterConfig.Location) - if err != nil { - return fmt.Errorf("error creating or updating network security group: %v", err) + if !scope.ClusterConfig.EtcdCAKeyPair.HasCertAndKey() { + etcdCACert, etcdCAKey, err := actuators.GetOrGenerateKeyPair(&scope.ClusterConfig.EtcdCAKeyPair, actuators.EtcdCA) + if err != nil { + return errors.Wrapf(err, "failed to get or generate etcd CA cert") + } + scope.ClusterConfig.EtcdCAKeyPair = v1alpha1.KeyPair{ + Cert: etcdCACert, + Key: etcdCAKey, + } } - err = networkSvc.WaitForNetworkSGsCreateOrUpdateFuture(*networkSGFuture) - if err != nil { - return fmt.Errorf("error waiting for network security group creation or update: %v", err) + + if !scope.ClusterConfig.FrontProxyCAKeyPair.HasCertAndKey() { + fpCACert, fpCAKey, err := actuators.GetOrGenerateKeyPair(&scope.ClusterConfig.FrontProxyCAKeyPair, actuators.FrontProxyCA) + if err != nil { + return errors.Wrapf(err, "failed to get or generate front-proxy CA cert") + } + scope.ClusterConfig.FrontProxyCAKeyPair = v1alpha1.KeyPair{ + Cert: fpCACert, + Key: fpCAKey, + } } - // Reconcile virtual network - vnetFuture, err := networkSvc.CreateOrUpdateVnet(scope.ClusterConfig.ResourceGroup, "", scope.ClusterConfig.Location) - if err != nil { - return fmt.Errorf("error creating or updating virtual network: %v", err) + if !scope.ClusterConfig.SAKeyPair.HasCertAndKey() { + saPub, saKey, err := actuators.GetOrGenerateKeyPair(&scope.ClusterConfig.SAKeyPair, actuators.ServiceAccount) + if err != nil { + return errors.Wrapf(err, "failed to get or generate service-account certificates") + } + scope.ClusterConfig.SAKeyPair = v1alpha1.KeyPair{ + Cert: saPub, + Key: saKey, + } } - err = networkSvc.WaitForVnetCreateOrUpdateFuture(*vnetFuture) - if err != nil { - return fmt.Errorf("error waiting for virtual network creation or update: %v", err) + + if err := resourcesSvc.ReconcileResourceGroup(); err != nil { + return errors.Errorf("unable to reconcile resource group: %+v", err) + } + + if err := networkSvc.ReconcileNetwork(); err != nil { + return errors.Errorf("unable to reconcile network: %+v", err) } + + // TODO: Add bastion method + /* + if err := resourcesSvc.ReconcileBastion(); err != nil { + return errors.Errorf("unable to reconcile bastion: %+v", err) + } + */ + + if err := networkSvc.ReconcileLoadBalancer("api"); err != nil { + return errors.Errorf("unable to reconcile load balancer: %+v", err) + } + return nil } @@ -102,23 +143,36 @@ func (a *Actuator) Delete(cluster *clusterv1.Cluster) error { defer scope.Close() + //networkSvc := network.NewService(scope) resourcesSvc := resources.NewService(scope) - resp, err := resourcesSvc.CheckGroupExistence(scope.ClusterConfig.ResourceGroup) - if err != nil { - return fmt.Errorf("error checking for resource group existence: %v", err) - } - if resp.StatusCode == 404 { - return fmt.Errorf("resource group %v does not exist", scope.ClusterConfig.ResourceGroup) + // TODO: Add load balancer method + /* + if err := networkSvc.DeleteLoadBalancers(); err != nil { + return errors.Errorf("unable to delete load balancers: %+v", err) + } + */ + + // TODO: Add bastion method + /* + if err := resourcesSvc.DeleteBastion(); err != nil { + return errors.Errorf("unable to delete bastion: %+v", err) + } + */ + + // TODO: Add network method + /* + if err := resourcesSvc.DeleteNetwork(); err != nil { + klog.Errorf("Error deleting cluster %v: %v.", cluster.Name, err) + return &controllerError.RequeueAfterError{ + RequeueAfter: 5 * 1000 * 1000 * 1000, + } + } + */ + + if err := resourcesSvc.DeleteResourceGroup(); err != nil { + return errors.Errorf("unable to delete resource group: %+v", err) } - groupsDeleteFuture, err := resourcesSvc.DeleteGroup(scope.ClusterConfig.ResourceGroup) - if err != nil { - return fmt.Errorf("error deleting resource group: %v", err) - } - err = resourcesSvc.WaitForGroupsDeleteFuture(groupsDeleteFuture) - if err != nil { - return fmt.Errorf("error waiting for resource group deletion: %v", err) - } return nil } diff --git a/pkg/cloud/azure/actuators/cluster/actuator_test.go b/pkg/cloud/azure/actuators/cluster/actuator_test.go index 8f243aef745..151d435f2c9 100644 --- a/pkg/cloud/azure/actuators/cluster/actuator_test.go +++ b/pkg/cloud/azure/actuators/cluster/actuator_test.go @@ -381,7 +381,7 @@ func TestClusterProviderFromProviderSpecParsingError(t *testing.T) { func newClusterProviderSpec() providerv1.AzureClusterProviderSpec { return providerv1.AzureClusterProviderSpec{ ResourceGroup: "resource-group-test", - Location: "eastus", + Location: "eastus2", } } diff --git a/pkg/cloud/azure/actuators/helpers.go b/pkg/cloud/azure/actuators/helpers.go new file mode 100644 index 00000000000..cb13debb27a --- /dev/null +++ b/pkg/cloud/azure/actuators/helpers.go @@ -0,0 +1,61 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package actuators + +import ( + "github.com/pkg/errors" + "k8s.io/klog" + "sigs.k8s.io/cluster-api-provider-azure/pkg/apis/azureprovider/v1alpha1" + "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/certificates" +) + +const ( + ClusterCA = "cluster-ca" + EtcdCA = "etcd-ca" + FrontProxyCA = "front-proxy-ca" + ServiceAccount = "service-account" +) + +// GetOrGenerateKeyPair returns a byte encoded cert and key pair if exists, generates one otherwise +func GetOrGenerateKeyPair(kp *v1alpha1.KeyPair, user string) ([]byte, []byte, error) { + if kp == nil || !kp.HasCertAndKey() { + klog.V(2).Infof("Generating key pair for %q", user) + switch user { + case EtcdCA, FrontProxyCA, ClusterCA: + x509Cert, privKey, err := certificates.NewCertificateAuthority() + if err != nil { + return nil, nil, errors.Wrapf(err, "failed to generate CA cert for %q", user) + } + return certificates.EncodeCertPEM(x509Cert), certificates.EncodePrivateKeyPEM(privKey), nil + case ServiceAccount: + saCreds, err := certificates.NewPrivateKey() + if err != nil { + return nil, nil, errors.Wrapf(err, "failed to create service account public and private keys") + } + saPub, err := certificates.EncodePublicKeyPEM(&saCreds.PublicKey) + if err != nil { + return nil, nil, errors.Wrapf(err, "failed to encode service account public key to PEM") + } + + return saPub, certificates.EncodePrivateKeyPEM(saCreds), nil + default: + return nil, nil, errors.Errorf("Unknown user %q, skipping generating keyPair", user) + } + } + + return kp.Cert, kp.Key, nil +} diff --git a/pkg/cloud/azure/actuators/helpers_test.go b/pkg/cloud/azure/actuators/helpers_test.go new file mode 100644 index 00000000000..373d481cd1f --- /dev/null +++ b/pkg/cloud/azure/actuators/helpers_test.go @@ -0,0 +1,150 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package actuators + +import ( + "testing" + + "github.com/pkg/errors" + "sigs.k8s.io/cluster-api-provider-azure/pkg/apis/azureprovider/v1alpha1" + "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/certificates" +) + +func TestGetOrGenerateKeyPair(t *testing.T) { + testCases := []struct { + name string + inputKeyPair *v1alpha1.KeyPair + inputUser string + expectKeyPairGen bool + expectCACertKeyPair bool + expectedError error + }{ + { + name: "should return generated \"cluster-ca\" keypair when inputKeyPair==nil", + inputKeyPair: nil, + inputUser: "cluster-ca", + expectKeyPairGen: true, + expectCACertKeyPair: true, + expectedError: nil, + }, + { + name: "should return generated \"cluster-ca\" keypair when inputKeyPair has no cert", + inputKeyPair: &v1alpha1.KeyPair{Key: []byte("foo-key")}, + inputUser: "cluster-ca", + expectKeyPairGen: true, + expectCACertKeyPair: true, + expectedError: nil, + }, + { + name: "should return generated \"cluster-ca\" keypair when inputKeyPair has no key", + inputKeyPair: &v1alpha1.KeyPair{Cert: []byte("foo-cert")}, + inputUser: "cluster-ca", + expectKeyPairGen: true, + expectCACertKeyPair: true, + expectedError: nil, + }, + { + name: "should generate \"cluster-ca\" keypair", + inputKeyPair: nil, + inputUser: "cluster-ca", + expectKeyPairGen: true, + expectCACertKeyPair: true, + expectedError: nil, + }, + { + name: "should generate \"etcd-ca\" keypair", + inputKeyPair: nil, + inputUser: "etcd-ca", + expectKeyPairGen: true, + expectCACertKeyPair: true, + expectedError: nil, + }, + { + name: "should generate \"front-proxy-ca\" keypair", + inputKeyPair: nil, + inputUser: "front-proxy-ca", + expectKeyPairGen: true, + expectCACertKeyPair: true, + expectedError: nil, + }, + { + name: "should generate \"service-account\" keypair", + inputKeyPair: nil, + inputUser: "service-account", + expectKeyPairGen: true, + expectCACertKeyPair: false, + expectedError: nil, + }, + { + name: "should return error for unknown user", + inputKeyPair: nil, + inputUser: "foo-ca", + expectKeyPairGen: true, + expectCACertKeyPair: false, + expectedError: errors.Errorf("Unknown user \"foo-ca\", skipping generating keyPair"), + }, + { + name: "should not generate keypair when inputKeyPair has cert and key", + inputKeyPair: &v1alpha1.KeyPair{Cert: []byte("foo-cert"), Key: []byte("foo-key")}, + inputUser: "cluster-ca", + expectKeyPairGen: false, + expectCACertKeyPair: false, + expectedError: nil, + }, + } + + for _, tc := range testCases { + actualCert, actualKey, actualError := GetOrGenerateKeyPair(tc.inputKeyPair, tc.inputUser) + if tc.expectedError != nil { + if tc.expectedError.Error() != actualError.Error() { + t.Fatalf("[%s], Unexpected error, Want [%v], Got: [%v]", tc.name, tc.expectedError, actualError) + } else { + continue + } + } + + if !tc.expectKeyPairGen { + if len(tc.inputKeyPair.Cert) != len(actualCert) || string(tc.inputKeyPair.Cert) != string(actualCert) { + t.Fatalf("[%s] Want cert=%q, Got cert=%q", tc.name, string(tc.inputKeyPair.Cert), string(actualCert)) + } + if len(tc.inputKeyPair.Key) != len(actualKey) || string(tc.inputKeyPair.Key) != string(actualKey) { + t.Fatalf("[%s] Want key=%q, Got key=%q", tc.name, string(tc.inputKeyPair.Key), string(actualKey)) + } + } else { + if tc.expectCACertKeyPair { + _, decodeErr := certificates.DecodeCertPEM(actualCert) + if decodeErr != nil { + t.Fatalf("[%s], Expected to decode generated cert, Got decode failure %v", tc.name, decodeErr) + } + _, decodeErr = certificates.DecodePrivateKeyPEM(actualKey) + if decodeErr != nil { + t.Fatalf("[%s], Expected to decode generated private key, Got decode failure failed %v", tc.name, decodeErr) + } + } else { + _, decodeErr := certificates.DecodePrivateKeyPEM(actualKey) + if decodeErr != nil { + t.Fatalf("[%s], Expected to decode generated private key, Got decode failure failed %v", tc.name, decodeErr) + } + + // TODO: find a stronger check + if len(actualCert) <= 0 { + t.Fatalf("[%s], Expected to public key of length > 0, Got public key of lenght %d", tc.name, len(actualCert)) + } + } + } + } +} diff --git a/pkg/cloud/azure/actuators/machine/BUILD.bazel b/pkg/cloud/azure/actuators/machine/BUILD.bazel index 4680a4e35d6..07a1a2526d2 100644 --- a/pkg/cloud/azure/actuators/machine/BUILD.bazel +++ b/pkg/cloud/azure/actuators/machine/BUILD.bazel @@ -15,12 +15,17 @@ go_library( "//pkg/cloud/azure/services/network:go_default_library", "//pkg/cloud/azure/services/resources:go_default_library", "//pkg/deployer:go_default_library", + "//pkg/tokens:go_default_library", "//vendor/github.com/pkg/errors:go_default_library", - "//vendor/github.com/pkg/sftp:go_default_library", "//vendor/golang.org/x/crypto/ssh:go_default_library", + "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", + "//vendor/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library", + "//vendor/k8s.io/client-go/tools/clientcmd:go_default_library", + "//vendor/k8s.io/client-go/tools/clientcmd/api:go_default_library", "//vendor/k8s.io/klog:go_default_library", "//vendor/sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1:go_default_library", "//vendor/sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1:go_default_library", + "//vendor/sigs.k8s.io/cluster-api/pkg/controller/error:go_default_library", ], ) diff --git a/pkg/cloud/azure/actuators/machine/actuator.go b/pkg/cloud/azure/actuators/machine/actuator.go index 07bc16fea31..54706910885 100644 --- a/pkg/cloud/azure/actuators/machine/actuator.go +++ b/pkg/cloud/azure/actuators/machine/actuator.go @@ -18,17 +18,17 @@ package machine import ( "context" - "encoding/base64" "fmt" - "io/ioutil" - "os" "reflect" "strings" "time" "github.com/pkg/errors" - "github.com/pkg/sftp" "golang.org/x/crypto/ssh" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + corev1 "k8s.io/client-go/kubernetes/typed/core/v1" + "k8s.io/client-go/tools/clientcmd" + clientcmdapi "k8s.io/client-go/tools/clientcmd/api" "k8s.io/klog" "sigs.k8s.io/cluster-api-provider-azure/pkg/apis/azureprovider/v1alpha1" "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/actuators" @@ -36,8 +36,10 @@ import ( "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/network" "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/resources" "sigs.k8s.io/cluster-api-provider-azure/pkg/deployer" + "sigs.k8s.io/cluster-api-provider-azure/pkg/tokens" clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1" client "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1" + controllerError "sigs.k8s.io/cluster-api/pkg/controller/error" ) //+kubebuilder:rbac:groups=azureprovider.k8s.io,resources=azuremachineproviderconfigs;azuremachineproviderstatuses,verbs=get;list;watch;create;update;patch;delete @@ -73,6 +75,57 @@ const ( SSHUser = "ClusterAPI" ) +func (a *Actuator) getControlPlaneMachines(machineList *clusterv1.MachineList) []*clusterv1.Machine { + var cpm []*clusterv1.Machine + for _, m := range machineList.Items { + if m.Spec.Versions.ControlPlane != "" { + cpm = append(cpm, m.DeepCopy()) + } + } + return cpm +} + +// defining equality as name and namespace are equivalent and not checking any other fields. +func machinesEqual(m1 *clusterv1.Machine, m2 *clusterv1.Machine) bool { + return m1.Name == m2.Name && m1.Namespace == m2.Namespace +} + +func (a *Actuator) isNodeJoin(controlPlaneMachines []*clusterv1.Machine, newMachine *clusterv1.Machine, cluster *clusterv1.Cluster) (bool, error) { + switch newMachine.ObjectMeta.Labels["set"] { + case "node": + return true, nil + case "controlplane": + contolPlaneExists := false + for _, cm := range controlPlaneMachines { + m, err := actuators.NewMachineScope(actuators.MachineScopeParams{ + Machine: cm, + Cluster: cluster, + Client: a.client, + }) + if err != nil { + return false, errors.Wrapf(err, "failed to create machine scope for machine %q", cm.Name) + } + + computeSvc := compute.NewService(m.Scope) + contolPlaneExists, err = computeSvc.MachineExists(m) + if err != nil { + return false, errors.Wrapf(err, "failed to verify existence of machine %q", m.Name()) + } + if contolPlaneExists { + break + } + } + + klog.V(2).Infof("Machine %q should join the controlplane: %t", newMachine.Name, contolPlaneExists) + return contolPlaneExists, nil + default: + errMsg := fmt.Sprintf("Unknown value %q for label \"set\" on machine %q, skipping machine creation", newMachine.ObjectMeta.Labels["set"], newMachine.Name) + klog.Errorf(errMsg) + err := errors.Errorf(errMsg) + return false, err + } +} + // Create creates a machine and is invoked by the machine controller. func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) error { klog.Infof("Creating machine %v for cluster %v", machine.Name, cluster.Name) @@ -84,33 +137,192 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi defer scope.Close() + computeSvc := compute.NewService(scope.Scope) + + clusterMachines, err := scope.MachineClient.List(v1.ListOptions{}) + if err != nil { + return errors.Wrapf(err, "failed to retrieve machines in cluster %q", cluster.Name) + } + controlPlaneMachines := a.getControlPlaneMachines(clusterMachines) + isNodeJoin, err := a.isNodeJoin(controlPlaneMachines, machine, cluster) + if err != nil { + return errors.Wrapf(err, "failed to determine whether machine %q should join cluster %q", machine.Name, cluster.Name) + } + + var bootstrapToken string + if isNodeJoin { + bootstrapToken, err = a.getNodeJoinToken(cluster) + if err != nil { + return errors.Wrapf(err, "failed to obtain token for node %q to join cluster %q", machine.Name, cluster.Name) + } + } + + kubeConfig := "" + + if len(controlPlaneMachines) > 0 { + kubeConfig, err = a.GetKubeConfig(cluster, nil) + if err != nil { + return errors.Wrapf(err, "failed to retrieve kubeconfig while creating machine %q", machine.Name) + } + } + + i, err := computeSvc.CreateOrGetMachine(scope, bootstrapToken, kubeConfig) + if err != nil { + klog.Errorf("network not ready to launch instances yet: %+v", err) + return &controllerError.RequeueAfterError{ + RequeueAfter: time.Minute, + } + + //return errors.Errorf("failed to create or get machine: %+v", err) + } + + scope.MachineStatus.VMID = &i.ID + + // TODO: Is this still required with scope.Close()? + //return a.updateAnnotations(cluster, machine) + + // TODO: update once machine controllers have a way to indicate a machine has been provisoned. https://github.com/kubernetes-sigs/cluster-api/issues/253 + // Seeing a node cannot be purely relied upon because the provisioned control plane will not be registering with + // the stack that provisions it. + if scope.MachineStatus.Annotations == nil { + scope.MachineStatus.Annotations = map[string]string{} + } + + scope.MachineStatus.Annotations["cluster-api-provider-azure"] = "true" + + return nil +} + +func (a *Actuator) getNodeJoinToken(cluster *clusterv1.Cluster) (string, error) { + controlPlaneDNSName, err := a.GetIP(cluster, nil) + if err != nil { + return "", errors.Errorf("failed to retrieve controlplane (GetIP): %+v", err) + } + + controlPlaneURL := fmt.Sprintf("https://%s:6443", controlPlaneDNSName) + + kubeConfig, err := a.GetKubeConfig(cluster, nil) + if err != nil { + return "", errors.Wrapf(err, "failed to retrieve kubeconfig for cluster %q.", cluster.Name) + } + + clientConfig, err := clientcmd.BuildConfigFromKubeconfigGetter(controlPlaneURL, func() (*clientcmdapi.Config, error) { + return clientcmd.Load([]byte(kubeConfig)) + }) + + if err != nil { + return "", errors.Wrapf(err, "failed to get client config for cluster at %q", controlPlaneURL) + } + + coreClient, err := corev1.NewForConfig(clientConfig) + if err != nil { + return "", errors.Wrapf(err, "failed to initialize new corev1 client") + } + + bootstrapToken, err := tokens.NewBootstrap(coreClient, 10*time.Minute) + if err != nil { + return "", errors.Wrapf(err, "failed to create new bootstrap token") + } + + return bootstrapToken, nil +} + +// Delete an existing machine based on the cluster and machine spec passed. +// Will block until the machine has been successfully deleted, or an error is returned. +func (a *Actuator) Delete(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) error { + klog.Infof("Deleting machine %v for cluster %v.", machine.Name, cluster.Name) + + scope, err := actuators.NewMachineScope(actuators.MachineScopeParams{Machine: machine, Cluster: cluster, Client: a.client}) + if err != nil { + return errors.Errorf("failed to create scope: %+v", err) + } + + defer scope.Close() + + computeSvc := compute.NewService(scope.Scope) + networkSvc := network.NewService(scope.Scope) resourcesSvc := resources.NewService(scope.Scope) - err = resourcesSvc.ValidateDeployment(machine, scope.ClusterConfig, scope.MachineConfig) + // Check if VM exists + vm, err := computeSvc.VMIfExists(resourcesSvc.GetVMName(machine)) + if err != nil { + return fmt.Errorf("error checking if vm exists: %v", err) + } + if vm == nil { + return fmt.Errorf("couldn't find vm for machine: %v", machine.Name) + } + + avm, err := scope.VM.Get(scope.Context, scope.ClusterConfig.ResourceGroup, vm.Name, "") if err != nil { - return fmt.Errorf("error validating deployment: %v", err) + return errors.Errorf("could not set vm: %v", err) } - deploymentsFuture, err := resourcesSvc.CreateOrUpdateDeployment(machine, scope.ClusterConfig, scope.MachineConfig) + osDiskName := avm.VirtualMachineProperties.StorageProfile.OsDisk.Name + nicID := (*avm.VirtualMachineProperties.NetworkProfile.NetworkInterfaces)[0].ID + + // delete the VM instance + vmDeleteFuture, err := computeSvc.DeleteVM(scope.ClusterConfig.ResourceGroup, resourcesSvc.GetVMName(machine)) if err != nil { - return fmt.Errorf("error creating or updating deployment: %v", err) + return fmt.Errorf("error deleting virtual machine: %v", err) } - err = resourcesSvc.WaitForDeploymentsCreateOrUpdateFuture(*deploymentsFuture) + err = computeSvc.WaitForVMDeletionFuture(vmDeleteFuture) if err != nil { - return fmt.Errorf("error waiting for deployment creation or update: %v", err) + return fmt.Errorf("error waiting for virtual machine deletion: %v", err) } - deployment, err := resourcesSvc.GetDeploymentResult(*deploymentsFuture) - // Work around possible bugs or late-stage failures - if deployment.Name == nil || err != nil { - return fmt.Errorf("error getting deployment result: %v", err) + // delete OS disk associated with the VM + diskDeleteFuture, err := computeSvc.DeleteManagedDisk(scope.ClusterConfig.ResourceGroup, *osDiskName) + if err != nil { + return fmt.Errorf("error deleting managed disk: %v", err) + } + err = computeSvc.WaitForDisksDeleteFuture(diskDeleteFuture) + if err != nil { + return fmt.Errorf("error waiting for managed disk deletion: %v", err) + } + + // delete NIC associated with the VM + nicName, err := resources.ResourceName(*nicID) + if err != nil { + return fmt.Errorf("error retrieving network interface name: %v", err) + } + interfacesDeleteFuture, err := networkSvc.DeleteNetworkInterface(scope.ClusterConfig.ResourceGroup, nicName) + if err != nil { + return fmt.Errorf("error deleting network interface: %v", err) + } + err = networkSvc.WaitForNetworkInterfacesDeleteFuture(interfacesDeleteFuture) + if err != nil { + return fmt.Errorf("error waiting for network interface deletion: %v", err) + } + + // delete public ip address associated with the VM + publicIPAddressDeleteFuture, err := networkSvc.DeletePublicIPAddress(scope.ClusterConfig.ResourceGroup, resourcesSvc.GetPublicIPName(machine)) + if err != nil { + return fmt.Errorf("error deleting public IP address: %v", err) + } + err = networkSvc.WaitForPublicIPAddressDeleteFuture(publicIPAddressDeleteFuture) + if err != nil { + return fmt.Errorf("error waiting for public ip address deletion: %v", err) } - // TODO: Is this still required with scope.Close()? - //return a.updateAnnotations(cluster, machine) return nil } -// Update an existing machine based on the cluster and machine spec parameters. +// TODO: Implement method +// isMachineOudated checks that no immutable fields have been updated in an +// Update request. +// Returns a bool indicating if an attempt to change immutable state occurred. +// - true: An attempt to change immutable state occurred. +// - false: Immutable state was untouched. +/* +func (a *Actuator) isMachineOutdated(machineSpec *v1alpha1.AzureMachineProviderSpec, instance *v1alpha1.VM) bool { + + // No immutable state changes found. + return false +} +*/ + +// Update updates a machine and is invoked by the Machine Controller. +// If the Update attempts to mutate any immutable state, the method will error +// and no updates will be performed. func (a *Actuator) Update(ctx context.Context, cluster *clusterv1.Cluster, goalMachine *clusterv1.Machine) error { klog.Infof("Updating machine %v for cluster %v.", goalMachine.Name, cluster.Name) @@ -122,6 +334,7 @@ func (a *Actuator) Update(ctx context.Context, cluster *clusterv1.Cluster, goalM defer scope.Close() computeSvc := compute.NewService(scope.Scope) + resourcesSvc := resources.NewService(scope.Scope) /* status, err := a.status(goalMachine) @@ -134,7 +347,7 @@ func (a *Actuator) Update(ctx context.Context, cluster *clusterv1.Cluster, goalM currentMachine := scope.Machine if currentMachine == nil { - vm, err := computeSvc.VMIfExists(scope.ClusterConfig.ResourceGroup, resources.GetVMName(goalMachine)) + vm, err := computeSvc.VMIfExists(resourcesSvc.GetVMName(goalMachine)) if err != nil || vm == nil { return fmt.Errorf("error checking if vm exists: %v", err) } @@ -177,6 +390,56 @@ func (a *Actuator) Update(ctx context.Context, cluster *clusterv1.Cluster, goalM return nil } +// Exists test for the existence of a machine and is invoked by the Machine Controller +func (a *Actuator) Exists(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) (bool, error) { + klog.Infof("Checking if machine %v for cluster %v exists", machine.Name, cluster.Name) + + scope, err := actuators.NewMachineScope(actuators.MachineScopeParams{Machine: machine, Cluster: cluster, Client: a.client}) + if err != nil { + return false, errors.Errorf("failed to create scope: %+v", err) + } + + defer scope.Close() + + computeSvc := compute.NewService(scope.Scope) + + // TODO worry about pointers. instance if exists returns *any* instance + if scope.MachineStatus.VMID == nil { + return false, nil + } + + instance, err := computeSvc.VMIfExists(*scope.MachineStatus.VMID) + if err != nil { + return false, errors.Errorf("failed to retrieve instance: %+v", err) + } + + if instance == nil { + return false, nil + } + + klog.Infof("Found instance for machine %q: %v", machine.Name, instance) + + switch instance.State { + case v1alpha1.VMStateSucceeded: + klog.Infof("Machine %v is running", *scope.MachineStatus.VMID) + case v1alpha1.VMStateUpdating: + klog.Infof("Machine %v is updating", *scope.MachineStatus.VMID) + default: + return false, nil + } + + /* + if err := a.reconcileLBAttachment(scope, machine, instance); err != nil { + return true, err + } + */ + + return true, nil +} + +// Old methods + +// TODO: Remove old methods func (a *Actuator) updateMaster(cluster *clusterv1.Cluster, currentMachine *clusterv1.Machine, goalMachine *clusterv1.Machine) error { //klog.Infof("Creating machine %v for cluster %v", machine.Name, cluster.Name) @@ -188,6 +451,7 @@ func (a *Actuator) updateMaster(cluster *clusterv1.Cluster, currentMachine *clus defer scope.Close() computeSvc := compute.NewService(scope.Scope) + resourcesSvc := resources.NewService(scope.Scope) // update the control plane if currentMachine.Spec.Versions.ControlPlane != goalMachine.Spec.Versions.ControlPlane { @@ -201,7 +465,7 @@ func (a *Actuator) updateMaster(cluster *clusterv1.Cluster, currentMachine *clus cmd += fmt.Sprintf("curl -sSL https://dl.k8s.io/release/v%s/bin/linux/amd64/kubectl | "+ "sudo tee /usr/bin/kubectl > /dev/null;"+ "sudo chmod a+rx /usr/bin/kubectl;", goalMachine.Spec.Versions.ControlPlane) - commandRunFuture, err := computeSvc.RunCommand(scope.ClusterConfig.ResourceGroup, resources.GetVMName(goalMachine), cmd) + commandRunFuture, err := computeSvc.RunCommand(scope.ClusterConfig.ResourceGroup, resourcesSvc.GetVMName(goalMachine), cmd) if err != nil { return fmt.Errorf("error running command on vm: %v", err) } @@ -213,14 +477,14 @@ func (a *Actuator) updateMaster(cluster *clusterv1.Cluster, currentMachine *clus // update master and node packages if currentMachine.Spec.Versions.Kubelet != goalMachine.Spec.Versions.Kubelet { - nodeName := strings.ToLower(resources.GetVMName(goalMachine)) + nodeName := strings.ToLower(resourcesSvc.GetVMName(goalMachine)) // prepare node for maintenance cmd := fmt.Sprintf("sudo kubectl drain %s --kubeconfig /etc/kubernetes/admin.conf --ignore-daemonsets;"+ "sudo apt-get install kubelet=%s;", nodeName, goalMachine.Spec.Versions.Kubelet+"-00") // mark the node as schedulable cmd += fmt.Sprintf("sudo kubectl uncordon %s --kubeconfig /etc/kubernetes/admin.conf;", nodeName) - commandRunFuture, err := computeSvc.RunCommand(scope.ClusterConfig.ResourceGroup, resources.GetVMName(goalMachine), cmd) + commandRunFuture, err := computeSvc.RunCommand(scope.ClusterConfig.ResourceGroup, resourcesSvc.GetVMName(goalMachine), cmd) if err != nil { return fmt.Errorf("error running command on vm: %v", err) } @@ -239,134 +503,6 @@ func (a *Actuator) shouldUpdate(m1 *clusterv1.Machine, m2 *clusterv1.Machine) bo m1.ObjectMeta.Name != m2.ObjectMeta.Name } -// Delete an existing machine based on the cluster and machine spec passed. -// Will block until the machine has been successfully deleted, or an error is returned. -func (a *Actuator) Delete(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) error { - klog.Infof("Creating machine %v for cluster %v", machine.Name, cluster.Name) - - scope, err := actuators.NewMachineScope(actuators.MachineScopeParams{Machine: machine, Cluster: cluster, Client: a.client}) - if err != nil { - return errors.Errorf("failed to create scope: %+v", err) - } - - defer scope.Close() - - computeSvc := compute.NewService(scope.Scope) - networkSvc := network.NewService(scope.Scope) - - // Check if VM exists - vm, err := computeSvc.VMIfExists(scope.ClusterConfig.ResourceGroup, resources.GetVMName(machine)) - if err != nil { - return fmt.Errorf("error checking if vm exists: %v", err) - } - if vm == nil { - return fmt.Errorf("couldn't find vm for machine: %v", machine.Name) - } - osDiskName := vm.VirtualMachineProperties.StorageProfile.OsDisk.Name - nicID := (*vm.VirtualMachineProperties.NetworkProfile.NetworkInterfaces)[0].ID - - // delete the VM instance - vmDeleteFuture, err := computeSvc.DeleteVM(scope.ClusterConfig.ResourceGroup, resources.GetVMName(machine)) - if err != nil { - return fmt.Errorf("error deleting virtual machine: %v", err) - } - err = computeSvc.WaitForVMDeletionFuture(vmDeleteFuture) - if err != nil { - return fmt.Errorf("error waiting for virtual machine deletion: %v", err) - } - - // delete OS disk associated with the VM - diskDeleteFuture, err := computeSvc.DeleteManagedDisk(scope.ClusterConfig.ResourceGroup, *osDiskName) - if err != nil { - return fmt.Errorf("error deleting managed disk: %v", err) - } - err = computeSvc.WaitForDisksDeleteFuture(diskDeleteFuture) - if err != nil { - return fmt.Errorf("error waiting for managed disk deletion: %v", err) - } - - // delete NIC associated with the VM - nicName, err := resources.ResourceName(*nicID) - if err != nil { - return fmt.Errorf("error retrieving network interface name: %v", err) - } - interfacesDeleteFuture, err := networkSvc.DeleteNetworkInterface(scope.ClusterConfig.ResourceGroup, nicName) - if err != nil { - return fmt.Errorf("error deleting network interface: %v", err) - } - err = networkSvc.WaitForNetworkInterfacesDeleteFuture(interfacesDeleteFuture) - if err != nil { - return fmt.Errorf("error waiting for network interface deletion: %v", err) - } - - // delete public ip address associated with the VM - publicIPAddressDeleteFuture, err := networkSvc.DeletePublicIPAddress(scope.ClusterConfig.ResourceGroup, resources.GetPublicIPName(machine)) - if err != nil { - return fmt.Errorf("error deleting public IP address: %v", err) - } - err = networkSvc.WaitForPublicIPAddressDeleteFuture(publicIPAddressDeleteFuture) - if err != nil { - return fmt.Errorf("error waiting for public ip address deletion: %v", err) - } - return nil -} - -// GetKubeConfig gets the kubeconfig of a machine based on the cluster and machine spec passed. -// Has not been fully tested as k8s is not yet bootstrapped on created machines. -func (a *Actuator) GetKubeConfig(cluster *clusterv1.Cluster, machine *clusterv1.Machine) (string, error) { - klog.Infof("Creating machine %v for cluster %v", machine.Name, cluster.Name) - - scope, err := actuators.NewMachineScope(actuators.MachineScopeParams{Machine: machine, Cluster: cluster, Client: a.client}) - if err != nil { - return "", errors.Errorf("failed to create scope: %+v", err) - } - - defer scope.Close() - - networkSvc := network.NewService(scope.Scope) - - decoded, err := base64.StdEncoding.DecodeString(scope.MachineConfig.SSHPrivateKey) - privateKey := string(decoded) - if err != nil { - return "", err - } - - ip, err := networkSvc.GetPublicIPAddress(scope.ClusterConfig.ResourceGroup, resources.GetPublicIPName(machine)) - if err != nil { - return "", fmt.Errorf("error getting public ip address: %v ", err) - } - sshclient, err := GetSSHClient(*ip.IPAddress, privateKey) - if err != nil { - return "", fmt.Errorf("unable to get ssh client: %v", err) - } - sftpClient, err := sftp.NewClient(sshclient) - if err != nil { - return "", fmt.Errorf("Error setting sftp client: %s", err) - } - - remoteFile := fmt.Sprintf("/home/%s/.kube/config", SSHUser) - srcFile, err := sftpClient.Open(remoteFile) - if err != nil { - return "", fmt.Errorf("Error opening %s: %s", remoteFile, err) - } - - defer srcFile.Close() - dstFileName := "kubeconfig" - dstFile, err := os.Create(dstFileName) - if err != nil { - return "", fmt.Errorf("unable to write local kubeconfig: %v", err) - } - - defer dstFile.Close() - srcFile.WriteTo(dstFile) - - content, err := ioutil.ReadFile(dstFileName) - if err != nil { - return "", fmt.Errorf("unable to read local kubeconfig: %v", err) - } - return string(content), nil -} - // GetSSHClient returns an instance of ssh.Client from the host and private key passed. func GetSSHClient(host string, privatekey string) (*ssh.Client, error) { key := []byte(privatekey) @@ -386,34 +522,6 @@ func GetSSHClient(host string, privatekey string) (*ssh.Client, error) { return client, err } -// Exists determines whether a machine exists based on the cluster and machine spec passed. -func (a *Actuator) Exists(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) (bool, error) { - klog.Infof("Checking if machine %v for cluster %v exists", machine.Name, cluster.Name) - - scope, err := actuators.NewMachineScope(actuators.MachineScopeParams{Machine: machine, Cluster: cluster, Client: a.client}) - if err != nil { - return false, errors.Errorf("failed to create scope: %+v", err) - } - - defer scope.Close() - - computeSvc := compute.NewService(scope.Scope) - resourcesSvc := resources.NewService(scope.Scope) - - resp, err := resourcesSvc.CheckGroupExistence(scope.ClusterConfig.ResourceGroup) - if err != nil { - return false, err - } - if resp.StatusCode == 404 { - return false, nil - } - vm, err := computeSvc.VMIfExists(scope.ClusterConfig.ResourceGroup, resources.GetVMName(machine)) - if err != nil { - return false, err - } - return vm != nil, nil -} - func isMasterMachine(roles []v1alpha1.MachineRole) bool { for _, r := range roles { if r == v1alpha1.Master { diff --git a/pkg/cloud/azure/actuators/machine/actuator_test.go b/pkg/cloud/azure/actuators/machine/actuator_test.go index 698a1335165..a46c6b0c8d9 100644 --- a/pkg/cloud/azure/actuators/machine/actuator_test.go +++ b/pkg/cloud/azure/actuators/machine/actuator_test.go @@ -1166,7 +1166,7 @@ func TestGetKubeConfigBase64Error(t *testing.T) { func TestGetKubeConfigIPAddressFailure(t *testing.T) { networkMock := services.MockAzureNetworkClient{} - mergo.Merge(&networkMock, services.MockGetPublicIPAddressFailure()) + mergo.Merge(&networkMock, services.MockCreateOrUpdatePublicIPAddressFailure()) azureServicesClient := actuators.AzureClients{Network: &networkMock} params := ActuatorParams{Services: &azureServicesClient} @@ -1208,7 +1208,7 @@ func TestGetIPFailureClusterParsing(t *testing.T) { func TestGetKubeConfigValidPrivateKey(t *testing.T) { networkMock := services.MockAzureNetworkClient{} - mergo.Merge(&networkMock, services.MockGetPublicIPAddress("127.0.0.1")) + mergo.Merge(&networkMock, services.MockCreateOrUpdatePublicIPAddress("127.0.0.1")) azureServicesClient := actuators.AzureClients{Network: &networkMock} params := ActuatorParams{Services: &azureServicesClient} @@ -1228,7 +1228,7 @@ func TestGetKubeConfigValidPrivateKey(t *testing.T) { } func TestGetKubeConfigInvalidBase64(t *testing.T) { networkMock := services.MockAzureNetworkClient{} - mergo.Merge(&networkMock, services.MockGetPublicIPAddress("127.0.0.1")) + mergo.Merge(&networkMock, services.MockCreateOrUpdatePublicIPAddress("127.0.0.1")) azureServicesClient := actuators.AzureClients{Network: &networkMock} params := ActuatorParams{Services: &azureServicesClient} @@ -1249,7 +1249,7 @@ func TestGetKubeConfigInvalidBase64(t *testing.T) { } func TestGetKubeConfigInvalidPrivateKey(t *testing.T) { networkMock := services.MockAzureNetworkClient{} - mergo.Merge(&networkMock, services.MockGetPublicIPAddress("127.0.0.1")) + mergo.Merge(&networkMock, services.MockCreateOrUpdatePublicIPAddress("127.0.0.1")) azureServicesClient := actuators.AzureClients{Network: &networkMock} params := ActuatorParams{Services: &azureServicesClient} @@ -1270,7 +1270,7 @@ func TestGetKubeConfigInvalidPrivateKey(t *testing.T) { } func TestGetIPSuccess(t *testing.T) { networkMock := services.MockAzureNetworkClient{} - mergo.Merge(&networkMock, services.MockGetPublicIPAddress("127.0.0.1")) + mergo.Merge(&networkMock, services.MockCreateOrUpdatePublicIPAddress("127.0.0.1")) azureServicesClient := actuators.AzureClients{Network: &networkMock} params := ActuatorParams{Services: &azureServicesClient} @@ -1294,7 +1294,7 @@ func TestGetIPSuccess(t *testing.T) { func TestGetIPFailure(t *testing.T) { networkMock := services.MockAzureNetworkClient{} - mergo.Merge(&networkMock, services.MockGetPublicIPAddressFailure()) + mergo.Merge(&networkMock, services.MockCreateOrUpdatePublicIPAddressFailure()) azureServicesClient := actuators.AzureClients{Network: &networkMock} params := ActuatorParams{Services: &azureServicesClient} diff --git a/pkg/cloud/azure/converters/BUILD.bazel b/pkg/cloud/azure/converters/BUILD.bazel new file mode 100644 index 00000000000..43bd5ce702d --- /dev/null +++ b/pkg/cloud/azure/converters/BUILD.bazel @@ -0,0 +1,12 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["vm.go"], + importpath = "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/converters", + visibility = ["//visibility:public"], + deps = [ + "//pkg/apis/azureprovider/v1alpha1:go_default_library", + "//vendor/github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-10-01/compute:go_default_library", + ], +) diff --git a/pkg/cloud/azure/converters/vm.go b/pkg/cloud/azure/converters/vm.go new file mode 100644 index 00000000000..c26eb8cf0e7 --- /dev/null +++ b/pkg/cloud/azure/converters/vm.go @@ -0,0 +1,42 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package converters + +import ( + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-10-01/compute" + "sigs.k8s.io/cluster-api-provider-azure/pkg/apis/azureprovider/v1alpha1" +) + +func SDKToVM(v compute.VirtualMachine) *v1alpha1.VM { + i := &v1alpha1.VM{ + ID: *v.ID, + Name: *v.Name, + + // TODO: Add more conversions once types are updated. + //Identity: string(v.Identity), + //Tags: v.Tags, + //State: v1alpha1.VMState(*v.State.Name), + } + + /* + if len(v.Tags) > 0 { + i.Tags = TagsToMap(v.Tags) + } + */ + + return i +} diff --git a/pkg/cloud/azure/services/certificates/BUILD.bazel b/pkg/cloud/azure/services/certificates/BUILD similarity index 100% rename from pkg/cloud/azure/services/certificates/BUILD.bazel rename to pkg/cloud/azure/services/certificates/BUILD diff --git a/pkg/cloud/azure/services/certificates/certificates.go b/pkg/cloud/azure/services/certificates/certificates.go index 0ed17774bc4..ffa02cb8ae4 100644 --- a/pkg/cloud/azure/services/certificates/certificates.go +++ b/pkg/cloud/azure/services/certificates/certificates.go @@ -1,5 +1,5 @@ /* -Copyright 2018 The Kubernetes Authors. +Copyright 2019 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -205,6 +205,19 @@ func EncodePrivateKeyPEM(key *rsa.PrivateKey) []byte { return pem.EncodeToMemory(&block) } +// EncodePublicKeyPEM returns PEM-encoded public key data. +func EncodePublicKeyPEM(key *rsa.PublicKey) ([]byte, error) { + der, err := x509.MarshalPKIXPublicKey(key) + if err != nil { + return []byte{}, err + } + block := pem.Block{ + Type: "PUBLIC KEY", + Bytes: der, + } + return pem.EncodeToMemory(&block), nil +} + // DecodeCertPEM attempts to return a decoded certificate or nil // if the encoded input does not contain a certificate. func DecodeCertPEM(encoded []byte) (*x509.Certificate, error) { diff --git a/pkg/cloud/azure/services/compute/BUILD.bazel b/pkg/cloud/azure/services/compute/BUILD.bazel index 91b3e80f6da..e1d4e6dabbc 100644 --- a/pkg/cloud/azure/services/compute/BUILD.bazel +++ b/pkg/cloud/azure/services/compute/BUILD.bazel @@ -10,9 +10,17 @@ go_library( importpath = "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/compute", visibility = ["//visibility:public"], deps = [ + "//pkg/apis/azureprovider/v1alpha1:go_default_library", "//pkg/cloud/azure/actuators:go_default_library", + "//pkg/cloud/azure/converters:go_default_library", + "//pkg/cloud/azure/services/certificates:go_default_library", + "//pkg/cloud/azure/services/config:go_default_library", + "//pkg/cloud/azure/services/resources:go_default_library", + "//pkg/record:go_default_library", "//vendor/github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-10-01/compute:go_default_library", "//vendor/github.com/Azure/go-autorest/autorest:go_default_library", "//vendor/github.com/Azure/go-autorest/autorest/to:go_default_library", + "//vendor/github.com/pkg/errors:go_default_library", + "//vendor/k8s.io/klog:go_default_library", ], ) diff --git a/pkg/cloud/azure/services/compute/virtualmachines.go b/pkg/cloud/azure/services/compute/virtualmachines.go index c22ae2b3122..1ebb28ff7c7 100644 --- a/pkg/cloud/azure/services/compute/virtualmachines.go +++ b/pkg/cloud/azure/services/compute/virtualmachines.go @@ -17,23 +17,44 @@ limitations under the License. package compute import ( + "fmt" + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-10-01/compute" "github.com/Azure/go-autorest/autorest" "github.com/Azure/go-autorest/autorest/to" + "github.com/pkg/errors" + "k8s.io/klog" + "sigs.k8s.io/cluster-api-provider-azure/pkg/apis/azureprovider/v1alpha1" + "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/actuators" + "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/converters" + "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/certificates" + "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/config" + "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/resources" + "sigs.k8s.io/cluster-api-provider-azure/pkg/record" ) -// RunCommand executes a command on the VM. -func (s *Service) RunCommand(resoureGroup string, name string, cmd string) (compute.VirtualMachinesRunCommandFuture, error) { - cmdInput := compute.RunCommandInput{ - CommandID: to.StringPtr("RunShellScript"), - Script: to.StringSlicePtr([]string{cmd}), +// CreateOrGetMachine will either return an existing instance or create and return an instance. +func (s *Service) CreateOrGetMachine(machine *actuators.MachineScope, bootstrapToken, kubeConfig string) (*v1alpha1.VM, error) { + klog.V(2).Infof("Attempting to create or get machine %q", machine.Name()) + + // instance id exists, try to get it + if machine.MachineStatus.VMID != nil { + klog.V(2).Infof("Looking up machine %q by id %q", machine.Name(), *machine.MachineStatus.VMID) + + instance, err := s.VMIfExists(*machine.MachineStatus.VMID) + if err != nil { + return nil, errors.Wrapf(err, "failed to look up machine %q by id %q", machine.Name(), *machine.MachineStatus.VMID) + } else if err == nil && instance != nil { + return instance, nil + } } - return s.scope.AzureClients.VM.RunCommand(s.scope.Context, resoureGroup, name, cmdInput) + + return s.createVM(machine, bootstrapToken, kubeConfig) } -// VMIfExists returns the reference to the VM object if it exists. -func (s *Service) VMIfExists(resourceGroup string, name string) (*compute.VirtualMachine, error) { - vm, err := s.scope.AzureClients.VM.Get(s.scope.Context, resourceGroup, name, "") +// VMIfExists returns the existing instance or nothing if it doesn't exist. +func (s *Service) VMIfExists(name string) (*v1alpha1.VM, error) { + vm, err := s.scope.AzureClients.VM.Get(s.scope.Context, s.scope.ClusterConfig.ResourceGroup, name, "") if err != nil { if aerr, ok := err.(autorest.DetailedError); ok { if aerr.StatusCode.(int) == 404 { @@ -42,7 +63,142 @@ func (s *Service) VMIfExists(resourceGroup string, name string) (*compute.Virtua } return nil, err } - return &vm, nil + + return converters.SDKToVM(vm), nil +} + +// createVM creates a new Azure VM instance. +func (s *Service) createVM(machine *actuators.MachineScope, bootstrapToken, kubeConfig string) (*v1alpha1.VM, error) { + klog.V(2).Infof("Creating a new instance for machine %q", machine.Name()) + + input := &v1alpha1.VM{} + + if !s.scope.ClusterConfig.CAKeyPair.HasCertAndKey() { + return nil, errors.New("failed to run controlplane, missing CACertificate") + } + + // TODO: Renable this once load balancer is implemented + /* + if s.scope.Network().APIServerIP.IPAddress == "" { + return nil, errors.New("failed to run controlplane, APIServer IP not available") + } + */ + + caCertHash, err := certificates.GenerateCertificateHash(s.scope.ClusterConfig.CAKeyPair.Cert) + if err != nil { + return nil, err + } + + // apply values based on the role of the machine + switch machine.Role() { + case "controlplane": + // TODO: Check for existence of control plane subnet & ensure NSG is attached to subnet + + var cfg string + var err error + + if bootstrapToken != "" { + klog.V(2).Infof("Allowing machine %q to join control plane for cluster %q", machine.Name(), s.scope.Name()) + + cfg, err = config.JoinControlPlane(&config.ContolPlaneJoinInput{ + CACert: string(s.scope.ClusterConfig.CAKeyPair.Cert), + CAKey: string(s.scope.ClusterConfig.CAKeyPair.Key), + CACertHash: caCertHash, + EtcdCACert: string(s.scope.ClusterConfig.EtcdCAKeyPair.Cert), + EtcdCAKey: string(s.scope.ClusterConfig.EtcdCAKeyPair.Key), + FrontProxyCACert: string(s.scope.ClusterConfig.FrontProxyCAKeyPair.Cert), + FrontProxyCAKey: string(s.scope.ClusterConfig.FrontProxyCAKeyPair.Key), + SaCert: string(s.scope.ClusterConfig.SAKeyPair.Cert), + SaKey: string(s.scope.ClusterConfig.SAKeyPair.Key), + BootstrapToken: bootstrapToken, + // TODO: Fix LBAddress to retrieve LB DNS Name + LBAddress: s.scope.Network().APIServerIP.IPAddress, + }) + if err != nil { + return input, err + } + } else { + klog.V(2).Infof("Machine %q is the first controlplane machine for cluster %q", machine.Name(), s.scope.Name()) + if !s.scope.ClusterConfig.CAKeyPair.HasCertAndKey() { + return nil, errors.New("failed to run controlplane, missing CAPrivateKey") + } + + cfg, err = config.NewControlPlane(&config.ControlPlaneInput{ + CACert: string(s.scope.ClusterConfig.CAKeyPair.Cert), + CAKey: string(s.scope.ClusterConfig.CAKeyPair.Key), + EtcdCACert: string(s.scope.ClusterConfig.EtcdCAKeyPair.Cert), + EtcdCAKey: string(s.scope.ClusterConfig.EtcdCAKeyPair.Key), + FrontProxyCACert: string(s.scope.ClusterConfig.FrontProxyCAKeyPair.Cert), + FrontProxyCAKey: string(s.scope.ClusterConfig.FrontProxyCAKeyPair.Key), + SaCert: string(s.scope.ClusterConfig.SAKeyPair.Cert), + SaKey: string(s.scope.ClusterConfig.SAKeyPair.Key), + // TODO: Fix LBAddress to retrieve LB DNS Name + LBAddress: "fakelb.example.com", //s.scope.Network().APIServerIP.IPAddress, + ClusterName: s.scope.Name(), + PodSubnet: s.scope.Cluster.Spec.ClusterNetwork.Pods.CIDRBlocks[0], + ServiceSubnet: s.scope.Cluster.Spec.ClusterNetwork.Services.CIDRBlocks[0], + ServiceDomain: s.scope.Cluster.Spec.ClusterNetwork.ServiceDomain, + KubernetesVersion: machine.Machine.Spec.Versions.ControlPlane, + }) + + if err != nil { + return input, err + } + } + + input.StartupScript = cfg + + case "node": + // TODO: Check for existence of node subnet & ensure NSG is attached to subnet + + cfg, err := config.NewNode(&config.NodeInput{ + CACertHash: caCertHash, + BootstrapToken: bootstrapToken, + // TODO: Fix LBAddress to retrieve LB DNS Name + LBAddress: s.scope.Network().APIServerIP.IPAddress, + }) + + if err != nil { + return input, err + } + + input.StartupScript = cfg + + default: + return nil, errors.Errorf("Unknown node role %q", machine.Role()) + } + + // TODO: Set ssh key + + resourcesSvc := resources.NewService(s.scope) + err = resourcesSvc.ValidateDeployment(machine.Machine, s.scope.ClusterConfig, machine.MachineConfig, input.StartupScript) + if err != nil { + return nil, fmt.Errorf("error validating deployment: %v", err) + } + + deploymentsFuture, err := resourcesSvc.CreateOrUpdateDeployment(machine.Machine, s.scope.ClusterConfig, machine.MachineConfig, input.StartupScript) + if err != nil { + return nil, fmt.Errorf("error creating or updating deployment: %v", err) + } + + err = resourcesSvc.WaitForDeploymentsCreateOrUpdateFuture(*deploymentsFuture) + if err != nil { + return nil, fmt.Errorf("error waiting for deployment creation or update: %v", err) + } + + deployment, err := resourcesSvc.GetDeploymentResult(*deploymentsFuture) + // Work around possible bugs or late-stage failures + if deployment.Name == nil || err != nil { + return nil, fmt.Errorf("error getting deployment result: %v", err) + } + + vm, err := s.scope.VM.Get(s.scope.Context, machine.ClusterConfig.ResourceGroup, machine.Machine.Name, "") + if err != nil { + return nil, err + } + + record.Eventf(machine.Machine, "CreatedVM", "Created new %s vm with id %q", machine.Role(), vm.ID) + return converters.SDKToVM(vm), nil } // DeleteVM deletes the virtual machine. @@ -50,6 +206,31 @@ func (s *Service) DeleteVM(resourceGroup string, name string) (compute.VirtualMa return s.scope.AzureClients.VM.Delete(s.scope.Context, resourceGroup, name) } +// MachineExists will return whether or not a machine exists. +func (s *Service) MachineExists(machine *actuators.MachineScope) (bool, error) { + var err error + var instance *v1alpha1.VM + if machine.MachineStatus.VMID != nil { + instance, err = s.VMIfExists(*machine.MachineStatus.VMID) + } + + if err != nil { + return false, errors.Wrapf(err, "failed to lookup machine %q", machine.Name()) + } + return instance != nil, nil +} + +// Old methods + +// RunCommand executes a command on the VM. +func (s *Service) RunCommand(resoureGroup string, name string, cmd string) (compute.VirtualMachinesRunCommandFuture, error) { + cmdInput := compute.RunCommandInput{ + CommandID: to.StringPtr("RunShellScript"), + Script: to.StringSlicePtr([]string{cmd}), + } + return s.scope.AzureClients.VM.RunCommand(s.scope.Context, resoureGroup, name, cmdInput) +} + // WaitForVMRunCommandFuture returns when the RunCommand operation completes. func (s *Service) WaitForVMRunCommandFuture(future compute.VirtualMachinesRunCommandFuture) error { return future.Future.WaitForCompletionRef(s.scope.Context, s.scope.AzureClients.VM.Client) diff --git a/pkg/cloud/azure/services/config/BUILD.bazel b/pkg/cloud/azure/services/config/BUILD.bazel new file mode 100644 index 00000000000..de61d9c1079 --- /dev/null +++ b/pkg/cloud/azure/services/config/BUILD.bazel @@ -0,0 +1,14 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = [ + "bastion.go", + "controlplane.go", + "node.go", + "template.go", + ], + importpath = "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/config", + visibility = ["//visibility:public"], + deps = ["//vendor/github.com/pkg/errors:go_default_library"], +) diff --git a/pkg/cloud/azure/services/config/bastion.go b/pkg/cloud/azure/services/config/bastion.go new file mode 100644 index 00000000000..188655020a4 --- /dev/null +++ b/pkg/cloud/azure/services/config/bastion.go @@ -0,0 +1,46 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package config + +const ( + // TODO: Make config Azure specific + bastionBashScript = `{{.Header}} + +BASTION_BOOTSTRAP_FILE=bastion_bootstrap.sh +BASTION_BOOTSTRAP=https://s3.amazonaws.com/aws-quickstart/quickstart-linux-bastion/scripts/bastion_bootstrap.sh + +curl -s -o $BASTION_BOOTSTRAP_FILE $BASTION_BOOTSTRAP +chmod +x $BASTION_BOOTSTRAP_FILE + +# This gets us far enough in the bastion script to be useful. +apt-get -y update && apt-get -y install python-pip +pip install --upgrade pip &> /dev/null + +./$BASTION_BOOTSTRAP_FILE --enable true +` +) + +// BastionInput defines the context to generate a bastion instance user data. +type BastionInput struct { + baseConfig +} + +// NewBastion returns the user data string to be used on a bastion instance. +func NewBastion(input *BastionInput) (string, error) { + input.Header = defaultHeader + return generate("bastion", bastionBashScript, input) +} diff --git a/pkg/cloud/azure/services/config/controlplane.go b/pkg/cloud/azure/services/config/controlplane.go new file mode 100644 index 00000000000..7cfacc1dc48 --- /dev/null +++ b/pkg/cloud/azure/services/config/controlplane.go @@ -0,0 +1,324 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package config + +import "github.com/pkg/errors" + +const ( + // TODO: Make config Azure specific + controlPlaneBashScript = `{{.Header}} + +set -eox + +mkdir -p /etc/kubernetes/pki/etcd + +echo -n '{{.CACert}}' > /etc/kubernetes/pki/ca.crt +echo -n '{{.CAKey}}' > /etc/kubernetes/pki/ca.key +chmod 600 /etc/kubernetes/pki/ca.key + +echo -n '{{.EtcdCACert}}' > /etc/kubernetes/pki/etcd/ca.crt +echo -n '{{.EtcdCAKey}}' > /etc/kubernetes/pki/etcd/ca.key +chmod 600 /etc/kubernetes/pki/etcd/ca.key + +echo -n '{{.FrontProxyCACert}}' > /etc/kubernetes/pki/front-proxy-ca.crt +echo -n '{{.FrontProxyCAKey}}' > /etc/kubernetes/pki/front-proxy-ca.key +chmod 600 /etc/kubernetes/pki/front-proxy-ca.key + +echo -n '{{.SaCert}}' > /etc/kubernetes/pki/sa.pub +echo -n '{{.SaKey}}' > /etc/kubernetes/pki/sa.key +chmod 600 /etc/kubernetes/pki/sa.key + +PRIVATE_IP=$(curl -H Metadata:true "http://169.254.169.254/metadata/instance/network/interface/0/ipv4/ipAddress/0/privateIpAddress?api-version=2018-10-01&format=text") +HOSTNAME="$(curl -H Metadata:true "http://169.254.169.254/metadata/instance/compute/name?api-version=2018-10-01&format=text")" + +cat >/tmp/kubeadm.yaml < /etc/sysctl.d/99-kubernetes-cri.conf </etc/apt/sources.list.d/kubernetes.list +deb https://apt.kubernetes.io/ kubernetes-xenial main +EOF +apt-get update +apt-get install -y kubelet kubeadm kubectl +apt-mark hold kubelet kubeadm kubectl + +kubeadm init --config /tmp/kubeadm.yaml --v 10 +` + + controlPlaneJoinBashScript = `{{.Header}} + +set -eox + +mkdir -p /etc/kubernetes/pki/etcd + +echo -n '{{.CACert}}' > /etc/kubernetes/pki/ca.crt +echo -n '{{.CAKey}}' > /etc/kubernetes/pki/ca.key +chmod 600 /etc/kubernetes/pki/ca.key + +echo -n '{{.EtcdCACert}}' > /etc/kubernetes/pki/etcd/ca.crt +echo -n '{{.EtcdCAKey}}' > /etc/kubernetes/pki/etcd/ca.key +chmod 600 /etc/kubernetes/pki/etcd/ca.key + +echo -n'{{.FrontProxyCACert}}' > /etc/kubernetes/pki/front-proxy-ca.crt +echo -n '{{.FrontProxyCAKey}}' > /etc/kubernetes/pki/front-proxy-ca.key +chmod 600 /etc/kubernetes/pki/front-proxy-ca.key + +echo -n '{{.SaCert}}' > /etc/kubernetes/pki/sa.pub +echo -n '{{.SaKey}}' > /etc/kubernetes/pki/sa.key +chmod 600 /etc/kubernetes/pki/sa.key + +PRIVATE_IP=$(curl -H Metadata:true "http://169.254.169.254/metadata/instance/network/interface/0/ipv4/ipAddress/0/privateIpAddress?api-version=2018-10-01&format=text") +HOSTNAME="$(curl -H Metadata:true "http://169.254.169.254/metadata/instance/compute/name?api-version=2018-10-01&format=text")" + +cat >/tmp/kubeadm-controlplane-join-config.yaml < /etc/sysctl.d/99-kubernetes-cri.conf </etc/apt/sources.list.d/kubernetes.list +deb https://apt.kubernetes.io/ kubernetes-xenial main +EOF +apt-get update +apt-get install -y kubelet kubeadm kubectl +apt-mark hold kubelet kubeadm kubectl + +kubeadm join --config /tmp/kubeadm-controlplane-join-config.yaml --v 10 +` +) + +func isKeyPairValid(cert, key string) bool { + return cert != "" && key != "" +} + +// ControlPlaneInput defines the context to generate a controlplane instance user data. +type ControlPlaneInput struct { + baseConfig + + CACert string + CAKey string + EtcdCACert string + EtcdCAKey string + FrontProxyCACert string + FrontProxyCAKey string + SaCert string + SaKey string + LBAddress string + ClusterName string + PodSubnet string + ServiceDomain string + ServiceSubnet string + KubernetesVersion string +} + +// ContolPlaneJoinInput defines context to generate controlplane instance user data for controlplane node join. +type ContolPlaneJoinInput struct { + baseConfig + + CACertHash string + CACert string + CAKey string + EtcdCACert string + EtcdCAKey string + FrontProxyCACert string + FrontProxyCAKey string + SaCert string + SaKey string + BootstrapToken string + LBAddress string +} + +func (cpi *ControlPlaneInput) validateCertificates() error { + if !isKeyPairValid(cpi.CACert, cpi.CAKey) { + return errors.New("CA cert material in the ControlPlaneInput is missing cert/key") + } + + if !isKeyPairValid(cpi.EtcdCACert, cpi.EtcdCAKey) { + return errors.New("ETCD CA cert material in the ControlPlaneInput is missing cert/key") + } + + if !isKeyPairValid(cpi.FrontProxyCACert, cpi.FrontProxyCAKey) { + return errors.New("FrontProxy CA cert material in ControlPlaneInput is missing cert/key") + } + + if !isKeyPairValid(cpi.SaCert, cpi.SaKey) { + return errors.New("ServiceAccount cert material in ControlPlaneInput is missing cert/key") + } + + return nil +} + +func (cpi *ContolPlaneJoinInput) validateCertificates() error { + if !isKeyPairValid(cpi.CACert, cpi.CAKey) { + return errors.New("CA cert material in the ContolPlaneJoinInput is missing cert/key") + } + + if !isKeyPairValid(cpi.EtcdCACert, cpi.EtcdCAKey) { + return errors.New("ETCD cert material in the ContolPlaneJoinInput is missing cert/key") + } + + if !isKeyPairValid(cpi.FrontProxyCACert, cpi.FrontProxyCAKey) { + return errors.New("FrontProxy cert material in ContolPlaneJoinInput is missing cert/key") + } + + if !isKeyPairValid(cpi.SaCert, cpi.SaKey) { + return errors.New("ServiceAccount cert material in ContolPlaneJoinInput is missing cert/key") + } + + return nil +} + +// NewControlPlane returns the user data string to be used on a controlplane instance. +func NewControlPlane(input *ControlPlaneInput) (string, error) { + input.Header = defaultHeader + if err := input.validateCertificates(); err != nil { + return "", errors.Wrapf(err, "ControlPlaneInput is invalid") + } + + config, err := generate("controlplane", controlPlaneBashScript, input) + if err != nil { + return "", errors.Wrapf(err, "failed to generate user data for new control plane machine") + } + + return config, err +} + +// JoinControlPlane returns the user data string to be used on a new contrplplane instance. +func JoinControlPlane(input *ContolPlaneJoinInput) (string, error) { + input.Header = defaultHeader + + if err := input.validateCertificates(); err != nil { + return "", errors.Wrapf(err, "ControlPlaneInput is invalid") + } + + config, err := generate("controlplane", controlPlaneJoinBashScript, input) + if err != nil { + return "", errors.Wrapf(err, "failed to generate user data for machine joining control plane") + } + return config, err +} diff --git a/pkg/cloud/azure/services/config/node.go b/pkg/cloud/azure/services/config/node.go new file mode 100644 index 00000000000..0ab83ff2684 --- /dev/null +++ b/pkg/cloud/azure/services/config/node.go @@ -0,0 +1,102 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package config + +const ( + // TODO: Make config Azure specific + nodeBashScript = `{{.Header}} + +HOSTNAME="$(curl -H Metadata:true "http://169.254.169.254/metadata/instance/compute/name?api-version=2018-10-01&format=text")" + +cat >/tmp/kubeadm-node.yaml < /etc/sysctl.d/99-kubernetes-cri.conf </etc/apt/sources.list.d/kubernetes.list +deb https://apt.kubernetes.io/ kubernetes-xenial main +EOF +apt-get update +apt-get install -y kubelet kubeadm kubectl +apt-mark hold kubelet kubeadm kubectl + +kubeadm join --config /tmp/kubeadm-node.yaml +` +) + +// NodeInput defines the context to generate a node user data. +type NodeInput struct { + baseConfig + + CACertHash string + BootstrapToken string + LBAddress string +} + +// NewNode returns the user data string to be used on a node instance. +func NewNode(input *NodeInput) (string, error) { + input.Header = defaultHeader + return generate("node", nodeBashScript, input) +} diff --git a/pkg/cloud/azure/services/config/template.go b/pkg/cloud/azure/services/config/template.go new file mode 100644 index 00000000000..8440bfcb71d --- /dev/null +++ b/pkg/cloud/azure/services/config/template.go @@ -0,0 +1,66 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package config + +import ( + "bytes" + "text/template" + + "github.com/pkg/errors" +) + +const ( + defaultHeader = `#!/usr/bin/env bash + +# Copyright 2019 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o verbose +set -o errexit +set -o nounset +set -o pipefail +` +) + +type baseConfig struct { + Header string +} + +func generate(kind string, tpl string, data interface{}) (string, error) { + t, err := template.New(kind).Parse(tpl) + if err != nil { + return "", errors.Wrapf(err, "failed to parse %s template", kind) + } + + var out bytes.Buffer + if err := t.Execute(&out, data); err != nil { + return "", errors.Wrapf(err, "failed to generate %s template", kind) + } + + return out.String(), nil +} diff --git a/pkg/cloud/azure/services/mock_interfaces.go b/pkg/cloud/azure/services/mock_interfaces.go index 3df8dcb8478..cf651a96c96 100644 --- a/pkg/cloud/azure/services/mock_interfaces.go +++ b/pkg/cloud/azure/services/mock_interfaces.go @@ -45,7 +45,7 @@ type MockAzureNetworkClient struct { MockNetworkSGIfExists func(resourceGroupName string, networkSecurityGroupName string) (*network.SecurityGroup, error) MockWaitForNetworkSGsCreateOrUpdateFuture func(future network.SecurityGroupsCreateOrUpdateFuture) error - MockGetPublicIPAddress func(resourceGroup string, IPName string) (network.PublicIPAddress, error) + MockCreateOrUpdatePublicIPAddress func(resourceGroup string, IPName string) (network.PublicIPAddress, error) MockDeletePublicIPAddress func(resourceGroup string, IPName string) (network.PublicIPAddressesDeleteFuture, error) MockWaitForPublicIPAddressDeleteFuture func(future network.PublicIPAddressesDeleteFuture) error @@ -138,12 +138,12 @@ func (m *MockAzureNetworkClient) WaitForNetworkInterfacesDeleteFuture(future net return m.MockWaitForNetworkInterfacesDeleteFuture(future) } -// GetPublicIPAddress retrieves the reference of the PublicIPAddress resource. -func (m *MockAzureNetworkClient) GetPublicIPAddress(resourceGroup string, IPName string) (network.PublicIPAddress, error) { - if m.MockGetPublicIPAddress == nil { +// CreateOrUpdatePublicIPAddress retrieves the reference of the PublicIPAddress resource. +func (m *MockAzureNetworkClient) CreateOrUpdatePublicIPAddress(resourceGroup string, IPName string) (network.PublicIPAddress, error) { + if m.MockCreateOrUpdatePublicIPAddress == nil { return network.PublicIPAddress{}, nil } - return m.MockGetPublicIPAddress(resourceGroup, IPName) + return m.MockCreateOrUpdatePublicIPAddress(resourceGroup, IPName) } // DeletePublicIPAddress deletes the PublicIPAddress resource. diff --git a/pkg/cloud/azure/services/mock_interfaces_helper.go b/pkg/cloud/azure/services/mock_interfaces_helper.go index 6ba52597479..158c4bdb45d 100644 --- a/pkg/cloud/azure/services/mock_interfaces_helper.go +++ b/pkg/cloud/azure/services/mock_interfaces_helper.go @@ -218,10 +218,10 @@ func MockPublicIPDeleteFutureFailure() MockAzureNetworkClient { } } -// MockGetPublicIPAddress mocks the GetPublicIPAddress success response. -func MockGetPublicIPAddress(ip string) MockAzureNetworkClient { +// MockCreateOrUpdatePublicIPAddress mocks the CreateOrUpdatePublicIPAddress success response. +func MockCreateOrUpdatePublicIPAddress(ip string) MockAzureNetworkClient { return MockAzureNetworkClient{ - MockGetPublicIPAddress: func(resourceGroup string, IPName string) (network.PublicIPAddress, error) { + MockCreateOrUpdatePublicIPAddress: func(resourceGroup string, IPName string) (network.PublicIPAddress, error) { publicIPAddress := network.PublicIPAddress{PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{}} publicIPAddress.IPAddress = to.StringPtr(ip) return publicIPAddress, nil @@ -229,10 +229,10 @@ func MockGetPublicIPAddress(ip string) MockAzureNetworkClient { } } -// MockGetPublicIPAddressFailure mocks the GetPublicIPAddress failure response. -func MockGetPublicIPAddressFailure() MockAzureNetworkClient { +// MockCreateOrUpdatePublicIPAddressFailure mocks the CreateOrUpdatePublicIPAddress failure response. +func MockCreateOrUpdatePublicIPAddressFailure() MockAzureNetworkClient { return MockAzureNetworkClient{ - MockGetPublicIPAddress: func(resourceGroup string, IPName string) (network.PublicIPAddress, error) { + MockCreateOrUpdatePublicIPAddress: func(resourceGroup string, IPName string) (network.PublicIPAddress, error) { return network.PublicIPAddress{}, errors.New("failed to get public ip address") }, } diff --git a/pkg/cloud/azure/services/network/BUILD.bazel b/pkg/cloud/azure/services/network/BUILD.bazel index 2142ea4caf6..ac1d687d1d9 100644 --- a/pkg/cloud/azure/services/network/BUILD.bazel +++ b/pkg/cloud/azure/services/network/BUILD.bazel @@ -4,6 +4,8 @@ go_library( name = "go_default_library", srcs = [ "interfaces.go", + "loadbalancer.go", + "network.go", "publicipaddress.go", "securitygroups.go", "service.go", @@ -16,5 +18,7 @@ go_library( "//vendor/github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-11-01/network:go_default_library", "//vendor/github.com/Azure/go-autorest/autorest:go_default_library", "//vendor/github.com/Azure/go-autorest/autorest/to:go_default_library", + "//vendor/github.com/pkg/errors:go_default_library", + "//vendor/k8s.io/klog:go_default_library", ], ) diff --git a/pkg/cloud/azure/services/network/loadbalancer.go b/pkg/cloud/azure/services/network/loadbalancer.go new file mode 100644 index 00000000000..278f18db8d8 --- /dev/null +++ b/pkg/cloud/azure/services/network/loadbalancer.go @@ -0,0 +1,241 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package network + +import ( + "fmt" + + "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-11-01/network" + "github.com/Azure/go-autorest/autorest/to" + "github.com/pkg/errors" + "k8s.io/klog" +) + +func (s *Service) ReconcileLoadBalancer(role string) error { + klog.V(2).Info("Reconciling load balancer") + + // Get default LB spec. + klog.V(2).Info("Getting load balancer spec") + spec, err := s.getLBSpec(role) + if err != nil { + klog.V(2).Info("Unable to get load balancer spec") + return err + } + + // Create or get a public IP + klog.V(2).Info("Getting or creating a public IP for load balancer") + pip, err := s.CreateOrUpdatePublicIPAddress(s.scope.ClusterConfig.ResourceGroup, to.String(s.setLBName(role))) + if err != nil { + return errors.Errorf("Public IP get/create was unsuccessful: %s", err) + } + klog.V(2).Info("Successfully retrieved a public IP for load balancer") + + klog.V(2).Info("Building frontend IP for load balancer") + frontendIPConfigs := []network.FrontendIPConfiguration{ + { + Name: spec.Name, + FrontendIPConfigurationPropertiesFormat: &network.FrontendIPConfigurationPropertiesFormat{ + PrivateIPAllocationMethod: network.Dynamic, + PublicIPAddress: &pip, + }, + }, + } + + // Describe or create a load balancer. + apiLB, err := s.describeLB(to.String(spec.Name)) + if err != nil { + klog.V(2).Info("Unable to find existing load balancer.") + spec.FrontendIPConfigurations = &frontendIPConfigs + + apiLB, err := s.createLB(*spec, role) + if err != nil { + return err + } + + klog.V(2).Infof("Created new load balancer for %s: %v", role, apiLB) + } + + apiLB, err = s.reconcileLBRules(&apiLB) + if err != nil { + return err + } + + klog.V(2).Infof("Control plane load balancer: %v", apiLB) + klog.V(2).Info("Reconcile load balancers completed successfully") + + return nil +} + +/* +func (s *Service) DeleteLoadBalancer() error { + +} +*/ + +func (s *Service) getLBSpec(role string) (*network.LoadBalancer, error) { + switch role { + case "api": + return s.getAPILBSpec(), nil + // TODO: Uncomment case once getServiceLBSpec exists + /* + case "service": + return s.getServiceLBSpec(), nil + */ + default: + return nil, errors.Errorf("No load balancer spec exists for %s", role) + } +} + +func (s *Service) getAPILBSpec() *network.LoadBalancer { + lbName := s.setLBName("api") + + res := &network.LoadBalancer{ + Name: lbName, + Location: &s.scope.ClusterConfig.Location, + Sku: &network.LoadBalancerSku{ + Name: network.LoadBalancerSkuNameStandard, + }, + LoadBalancerPropertiesFormat: &network.LoadBalancerPropertiesFormat{ + // TODO: Remove debug + /* + FrontendIPConfigurations: &[]network.FrontendIPConfiguration{ + { + Name: lbName, + }, + }, + */ + BackendAddressPools: &[]network.BackendAddressPool{ + { + Name: lbName, + }, + }, + Probes: &[]network.Probe{ + { + Name: lbName, + ProbePropertiesFormat: &network.ProbePropertiesFormat{ + Protocol: network.ProbeProtocolTCP, + Port: to.Int32Ptr(6443), + IntervalInSeconds: to.Int32Ptr(5), + NumberOfProbes: to.Int32Ptr(2), + }, + }, + }, + }, + //Tags: + } + + // TODO: Needs converter method + return res +} + +// TODO: Add getServiceLBSpec +/* +func (s *Service) getServiceLBSpec() *v1alpha1.LoadBalancer { +} +*/ + +func (s *Service) describeLB(lbName string) (network.LoadBalancer, error) { + klog.V(2).Info("Attempting to find existing load balancer") + return s.scope.LB.Get(s.scope.Context, s.scope.ClusterConfig.ResourceGroup, lbName, "") +} + +func (s *Service) createLB(lbSpec network.LoadBalancer, role string) (lb network.LoadBalancer, err error) { + klog.V(2).Info("Creating a new load balancer.") + future, err := s.scope.LB.CreateOrUpdate(s.scope.Context, s.scope.ClusterConfig.ResourceGroup, *lbSpec.Name, lbSpec) + + if err != nil { + return lb, err + } + + err = future.WaitForCompletionRef(s.scope.Context, s.scope.LB.Client) + if err != nil { + return lb, fmt.Errorf("cannot get load balancer create or update future response: %v", err) + } + + return future.Result(s.scope.LB) +} + +func (s *Service) reconcileLBRules(lbSpec *network.LoadBalancer) (lb network.LoadBalancer, err error) { + klog.V(2).Infof("Reconciling load balancer rules") + lbRules := []network.LoadBalancingRule{ + { + Name: to.StringPtr("api"), + LoadBalancingRulePropertiesFormat: &network.LoadBalancingRulePropertiesFormat{ + Protocol: network.TransportProtocolTCP, + FrontendPort: to.Int32Ptr(6443), + BackendPort: to.Int32Ptr(6443), + FrontendIPConfiguration: &network.SubResource{ + ID: to.StringPtr( + s.getResourceID( + *lbSpec.ID, + "frontendIPConfigurations", + *lbSpec.Name, + ), + ), + }, + BackendAddressPool: &network.SubResource{ + ID: to.StringPtr( + s.getResourceID( + *lbSpec.ID, + "backendAddressPools", + *lbSpec.Name, + ), + ), + }, + Probe: &network.SubResource{ + ID: to.StringPtr( + s.getResourceID( + *lbSpec.ID, + "probes", + *lbSpec.Name, + ), + ), + }, + }, + }, + } + + lbSpec.LoadBalancingRules = &lbRules + + future, err := s.scope.LB.CreateOrUpdate( + s.scope.Context, + s.scope.ClusterConfig.ResourceGroup, + to.String(lbSpec.Name), + *lbSpec, + ) + + if err != nil { + return lb, err + } + + err = future.WaitForCompletionRef(s.scope.Context, s.scope.LB.Client) + if err != nil { + return lb, fmt.Errorf("cannot get load balancer create or update future response: %v", err) + } + + klog.V(2).Info("Successfully reconciled load balancer rules") + return future.Result(s.scope.LB) +} + +func (s *Service) getResourceID(lbID, resource, resourceName string) string { + return fmt.Sprintf("%s/%s/%s", lbID, resource, resourceName) +} + +func (s *Service) setLBName(role string) *string { + str := fmt.Sprintf("%s-%s", s.scope.Cluster.Name, role) + return &str +} diff --git a/pkg/cloud/azure/services/network/network.go b/pkg/cloud/azure/services/network/network.go new file mode 100644 index 00000000000..d4fc0cfde5f --- /dev/null +++ b/pkg/cloud/azure/services/network/network.go @@ -0,0 +1,64 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package network + +import ( + "fmt" + + "k8s.io/klog" +) + +// ReconcileNetwork reconciles the network of the given cluster. +func (s *Service) ReconcileNetwork() (err error) { + klog.V(2).Info("Reconciling network") + + // TODO: Refactor + // TODO: Fix hardcoded values. + // Reconcile network security group + networkSGFuture, err := s.CreateOrUpdateNetworkSecurityGroup(s.scope.ClusterConfig.ResourceGroup, "ClusterAPINSG", s.scope.ClusterConfig.Location) + if err != nil { + return fmt.Errorf("error creating or updating network security group: %v", err) + } + err = s.WaitForNetworkSGsCreateOrUpdateFuture(*networkSGFuture) + if err != nil { + return fmt.Errorf("error waiting for network security group creation or update: %v", err) + } + + // Reconcile virtual network + vnetFuture, err := s.CreateOrUpdateVnet(s.scope.ClusterConfig.ResourceGroup, "", s.scope.ClusterConfig.Location) + if err != nil { + return fmt.Errorf("error creating or updating virtual network: %v", err) + } + err = s.WaitForVnetCreateOrUpdateFuture(*vnetFuture) + if err != nil { + return fmt.Errorf("error waiting for virtual network creation or update: %v", err) + } + + klog.V(2).Info("Reconcile network completed successfully") + return nil +} + +// TODO: Implement method +/* +// DeleteNetwork deletes the network of the given cluster. +func (s *Service) DeleteNetwork() (err error) { + klog.V(2).Info("Deleting network") + + klog.V(2).Info("Delete network completed successfully") + return nil +} +*/ diff --git a/pkg/cloud/azure/services/network/publicipaddress.go b/pkg/cloud/azure/services/network/publicipaddress.go index 7c5d23262ea..c395cb7ebbf 100644 --- a/pkg/cloud/azure/services/network/publicipaddress.go +++ b/pkg/cloud/azure/services/network/publicipaddress.go @@ -17,20 +17,60 @@ limitations under the License. package network import ( + "fmt" + "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-11-01/network" + "github.com/Azure/go-autorest/autorest/to" ) -// GetPublicIPAddress retrieves the Public IP address resource. -func (s *Service) GetPublicIPAddress(resourceGroup string, IPName string) (network.PublicIPAddress, error) { - return s.scope.AzureClients.PublicIPAddresses.Get(s.scope.Context, resourceGroup, IPName, "") +// CreateOrUpdatePublicIPAddress retrieves the Public IP address resource. +func (s *Service) CreateOrUpdatePublicIPAddress(resourceGroup string, IPName string) (pip network.PublicIPAddress, err error) { + future, err := s.scope.PublicIPAddresses.CreateOrUpdate( + s.scope.Context, + resourceGroup, + IPName, + network.PublicIPAddress{ + Name: to.StringPtr(IPName), + Location: to.StringPtr(s.scope.Location()), + Sku: s.getDefaultPublicIPSKU(), + PublicIPAddressPropertiesFormat: s.getDefaultPublicIPProperties(IPName), + }, + ) + + if err != nil { + return pip, err + } + + err = future.WaitForCompletionRef(s.scope.Context, s.scope.PublicIPAddresses.Client) + if err != nil { + return pip, fmt.Errorf("cannot get public ip address create or update future response: %v", err) + } + + return future.Result(s.scope.PublicIPAddresses) } // DeletePublicIPAddress deletes the Public IP address resource. func (s *Service) DeletePublicIPAddress(resourceGroup string, IPName string) (network.PublicIPAddressesDeleteFuture, error) { - return s.scope.AzureClients.PublicIPAddresses.Delete(s.scope.Context, resourceGroup, IPName) + return s.scope.PublicIPAddresses.Delete(s.scope.Context, resourceGroup, IPName) } // WaitForPublicIPAddressDeleteFuture waits for the DeletePublicIPAddress operation to complete. func (s *Service) WaitForPublicIPAddressDeleteFuture(future network.PublicIPAddressesDeleteFuture) error { - return future.Future.WaitForCompletionRef(s.scope.Context, s.scope.AzureClients.PublicIPAddresses.Client) + return future.Future.WaitForCompletionRef(s.scope.Context, s.scope.PublicIPAddresses.Client) +} + +func (s *Service) getDefaultPublicIPSKU() *network.PublicIPAddressSku { + return &network.PublicIPAddressSku{ + Name: network.PublicIPAddressSkuNameStandard, + } +} + +func (s *Service) getDefaultPublicIPProperties(IPName string) *network.PublicIPAddressPropertiesFormat { + return &network.PublicIPAddressPropertiesFormat{ + PublicIPAddressVersion: network.IPv4, + PublicIPAllocationMethod: network.Static, + DNSSettings: &network.PublicIPAddressDNSSettings{ + DomainNameLabel: &IPName, + }, + } } diff --git a/pkg/cloud/azure/services/resources/BUILD.bazel b/pkg/cloud/azure/services/resources/BUILD.bazel index 712440522de..f569ad56cd5 100644 --- a/pkg/cloud/azure/services/resources/BUILD.bazel +++ b/pkg/cloud/azure/services/resources/BUILD.bazel @@ -17,6 +17,7 @@ go_library( "//vendor/github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources:go_default_library", "//vendor/github.com/Azure/go-autorest/autorest:go_default_library", "//vendor/github.com/Azure/go-autorest/autorest/to:go_default_library", + "//vendor/k8s.io/klog:go_default_library", "//vendor/sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1:go_default_library", ], ) diff --git a/pkg/cloud/azure/services/resources/deployments.go b/pkg/cloud/azure/services/resources/deployments.go index 77015daf4d3..e1b7f14b968 100644 --- a/pkg/cloud/azure/services/resources/deployments.go +++ b/pkg/cloud/azure/services/resources/deployments.go @@ -24,6 +24,7 @@ import ( "io/ioutil" "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources" + "k8s.io/klog" providerv1 "sigs.k8s.io/cluster-api-provider-azure/pkg/apis/azureprovider/v1alpha1" "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/network" clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1" @@ -34,13 +35,19 @@ const ( ) // CreateOrUpdateDeployment is used to create or update a kubernetes cluster. It does so by creating or updating an ARM deployment. -func (s *Service) CreateOrUpdateDeployment(machine *clusterv1.Machine, clusterConfig *providerv1.AzureClusterProviderSpec, machineConfig *providerv1.AzureMachineProviderSpec) (*resources.DeploymentsCreateOrUpdateFuture, error) { +func (s *Service) CreateOrUpdateDeployment(machine *clusterv1.Machine, clusterConfig *providerv1.AzureClusterProviderSpec, machineConfig *providerv1.AzureMachineProviderSpec, startupScript string) (*resources.DeploymentsCreateOrUpdateFuture, error) { + // TODO: Remove debug + klog.V(2).Info("CreateOrUpdateDeployment start") // Parse the ARM template + // TODO: Remove debug + klog.V(2).Info("CreateOrUpdateDeployment: reading template") template, err := readJSON(templateFile) if err != nil { + // TODO: Remove debug + klog.V(2).Info("CreateOrUpdateDeployment: could not read template") return nil, err } - params, err := convertMachineToDeploymentParams(machine, machineConfig) + params, err := s.convertVMToDeploymentParams(machine, machineConfig, startupScript) if err != nil { return nil, err } @@ -60,13 +67,17 @@ func (s *Service) CreateOrUpdateDeployment(machine *clusterv1.Machine, clusterCo } // ValidateDeployment validates the parameters of the cluster by calling the ARM validate method. -func (s *Service) ValidateDeployment(machine *clusterv1.Machine, clusterConfig *providerv1.AzureClusterProviderSpec, machineConfig *providerv1.AzureMachineProviderSpec) error { +func (s *Service) ValidateDeployment(machine *clusterv1.Machine, clusterConfig *providerv1.AzureClusterProviderSpec, machineConfig *providerv1.AzureMachineProviderSpec, startupScript string) error { + // TODO: Remove debug + klog.V(2).Info("ValidateDeployment start") // Parse the ARM template template, err := readJSON(templateFile) + // TODO: Remove debug + klog.V(2).Info("ValidateDeployment: reading template") if err != nil { return err } - params, err := convertMachineToDeploymentParams(machine, machineConfig) + params, err := s.convertVMToDeploymentParams(machine, machineConfig, startupScript) if err != nil { return err } @@ -94,16 +105,18 @@ func (s *Service) WaitForDeploymentsCreateOrUpdateFuture(future resources.Deploy return future.WaitForCompletionRef(s.scope.Context, s.scope.AzureClients.Deployments.Client) } -func convertMachineToDeploymentParams(machine *clusterv1.Machine, machineConfig *providerv1.AzureMachineProviderSpec) (*map[string]interface{}, error) { - startupScript, err := getStartupScript(machine, *machineConfig) - if err != nil { - return nil, err - } +func (s *Service) convertVMToDeploymentParams(machine *clusterv1.Machine, machineConfig *providerv1.AzureMachineProviderSpec, startupScript string) (*map[string]interface{}, error) { + // TODO: Remove debug + klog.V(2).Info("convertVMToDeploymentParams start") decoded, err := base64.StdEncoding.DecodeString(machineConfig.SSHPublicKey) publicKey := string(decoded) if err != nil { + // TODO: Remove debug + klog.V(2).Info("convertVMToDeploymentParams: could not decode SSH key") return nil, err } + + // TODO: Allow parameterized value or set defaults params := map[string]interface{}{ "clusterAPI_machine_name": map[string]interface{}{ "value": machine.ObjectMeta.Name, @@ -112,13 +125,13 @@ func convertMachineToDeploymentParams(machine *clusterv1.Machine, machineConfig "value": network.VnetDefaultName, }, "virtualMachines_ClusterAPIVM_name": map[string]interface{}{ - "value": GetVMName(machine), + "value": s.GetVMName(machine), }, "networkInterfaces_ClusterAPI_name": map[string]interface{}{ - "value": GetNetworkInterfaceName(machine), + "value": s.GetNetworkInterfaceName(machine), }, "publicIPAddresses_ClusterAPI_ip_name": map[string]interface{}{ - "value": GetPublicIPName(machine), + "value": s.GetPublicIPName(machine), }, "networkSecurityGroups_ClusterAPIVM_nsg_name": map[string]interface{}{ "value": "ClusterAPINSG", @@ -139,7 +152,7 @@ func convertMachineToDeploymentParams(machine *clusterv1.Machine, machineConfig "value": machineConfig.Image.Version, }, "osDisk_name": map[string]interface{}{ - "value": GetOSDiskName(machine), + "value": s.GetOSDiskName(machine), }, "os_type": map[string]interface{}{ "value": machineConfig.OSDisk.OSType, @@ -151,7 +164,7 @@ func convertMachineToDeploymentParams(machine *clusterv1.Machine, machineConfig "value": machineConfig.OSDisk.DiskSizeGB, }, "vm_user": map[string]interface{}{ - "value": "ClusterAPI", + "value": "capi", }, "vm_size": map[string]interface{}{ "value": machineConfig.VMSize, @@ -224,7 +237,8 @@ mkdir -p /home/ClusterAPI/.kube cp -i /etc/kubernetes/admin.conf /home/ClusterAPI/.kube/config chown $(id -u ClusterAPI):$(id -g ClusterAPI) /home/ClusterAPI/.kube/config -KUBECONFIG=/etc/kubernetes/admin.conf kubectl apply -f https://raw.githubusercontent.com/cloudnativelabs/kube-router/master/daemonset/kubeadm-kuberouter.yaml 2>&1 | tee /var/log/startup.log`, machine.Spec.Versions.Kubelet, machine.Spec.Versions.ControlPlane) +KUBECONFIG=/etc/kubernetes/admin.conf kubectl apply -f https://raw.githubusercontent.com/cloudnativelabs/kube-router/master/daemonset/kubeadm-kuberouter.yaml +) 2>&1 | tee /var/log/startup.log`, machine.Spec.Versions.Kubelet, machine.Spec.Versions.ControlPlane) return startupScript, nil } else if machineConfig.Roles[0] == providerv1.Node { startupScript := fmt.Sprintf(`( @@ -256,24 +270,25 @@ kubeadm join --token "${TOKEN}" "${MASTER}" --ignore-preflight-errors=all --disc return "", errors.New("unable to get startup script: unknown machine role") } -// GetPublicIPName returns the public IP resource name of the machine. -func GetPublicIPName(machine *clusterv1.Machine) string { - return fmt.Sprintf("ClusterAPIIP-%s", machine.ObjectMeta.Name) +// GetVMName returns the VM resource name of the machine. +func (s *Service) GetVMName(machine *clusterv1.Machine) string { + return fmt.Sprintf("%s-%s", s.scope.Name(), machine.ObjectMeta.Name) } -// GetNetworkInterfaceName returns the nic resource name of the machine. -func GetNetworkInterfaceName(machine *clusterv1.Machine) string { - return fmt.Sprintf("ClusterAPINIC-%s", GetVMName(machine)) +// GetPublicIPName returns the public IP resource name of the machine. +// TODO: Move to network package +func (s *Service) GetPublicIPName(machine *clusterv1.Machine) string { + return fmt.Sprintf("%s-pip", s.GetVMName(machine)) } -// GetVMName returns the VM resource name of the machine. -func GetVMName(machine *clusterv1.Machine) string { - return fmt.Sprintf("ClusterAPIVM-%s", machine.ObjectMeta.Name) +// GetNetworkInterfaceName returns the nic resource name of the machine. +func (s *Service) GetNetworkInterfaceName(machine *clusterv1.Machine) string { + return fmt.Sprintf("%s-nic", s.GetVMName(machine)) } // GetOSDiskName returns the OS disk resource name of the machine. -func GetOSDiskName(machine *clusterv1.Machine) string { - return fmt.Sprintf("%s_OSDisk", GetVMName(machine)) +func (s *Service) GetOSDiskName(machine *clusterv1.Machine) string { + return fmt.Sprintf("%s_OSDisk", s.GetVMName(machine)) } func readJSON(path string) (*map[string]interface{}, error) { diff --git a/pkg/cloud/azure/services/resources/groups.go b/pkg/cloud/azure/services/resources/groups.go index f6397b38150..2817e23d5ad 100644 --- a/pkg/cloud/azure/services/resources/groups.go +++ b/pkg/cloud/azure/services/resources/groups.go @@ -17,11 +17,55 @@ limitations under the License. package resources import ( + "fmt" + "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources" "github.com/Azure/go-autorest/autorest" "github.com/Azure/go-autorest/autorest/to" + "k8s.io/klog" ) +// ReconcileResourceGroup reconciles the resource group of the given cluster. +func (s *Service) ReconcileResourceGroup() (err error) { + klog.V(2).Info("Reconciling resource group") + + // TODO: Refactor + // Reconcile resource group + _, err = s.CreateOrUpdateGroup(s.scope.ClusterConfig.ResourceGroup, s.scope.ClusterConfig.Location) + if err != nil { + return fmt.Errorf("failed to create or update resource group: %v", err) + } + + klog.V(2).Info("Reconciling resource group completed successfully") + return nil +} + +// DeleteResourceGroup deletes the network of the given cluster. +func (s *Service) DeleteResourceGroup() (err error) { + klog.V(2).Info("Deleting resource group") + + // TODO: Refactor + resp, err := s.CheckGroupExistence(s.scope.ClusterConfig.ResourceGroup) + if err != nil { + return fmt.Errorf("error checking for resource group existence: %v", err) + } + if resp.StatusCode == 404 { + return fmt.Errorf("resource group %v does not exist", s.scope.ClusterConfig.ResourceGroup) + } + + groupsDeleteFuture, err := s.DeleteGroup(s.scope.ClusterConfig.ResourceGroup) + if err != nil { + return fmt.Errorf("error deleting resource group: %v", err) + } + err = s.WaitForGroupsDeleteFuture(groupsDeleteFuture) + if err != nil { + return fmt.Errorf("error waiting for resource group deletion: %v", err) + } + + klog.V(2).Info("Deleting resource group completed successfully") + return nil +} + // CreateOrUpdateGroup creates or updates an azure resource group. func (s *Service) CreateOrUpdateGroup(resourceGroupName string, location string) (resources.Group, error) { return s.scope.AzureClients.Groups.CreateOrUpdate(s.scope.Context, resourceGroupName, resources.Group{Location: to.StringPtr(location)}) @@ -32,7 +76,7 @@ func (s *Service) DeleteGroup(resourceGroupName string) (resources.GroupsDeleteF return s.scope.AzureClients.Groups.Delete(s.scope.Context, resourceGroupName) } -// CheckGroupExistence checks oif the resource group exists or not. +// CheckGroupExistence checks if the resource group exists or not. func (s *Service) CheckGroupExistence(resourceGroupName string) (autorest.Response, error) { return s.scope.AzureClients.Groups.CheckExistence(s.scope.Context, resourceGroupName) } diff --git a/pkg/cloud/azure/services/resources/template/deployment-params.json b/pkg/cloud/azure/services/resources/template/deployment-params.json deleted file mode 100644 index bcea8c6e674..00000000000 --- a/pkg/cloud/azure/services/resources/template/deployment-params.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "clusterAPI_machine_name": { - "value": "ClusterAPIMachineName" - }, - "virtualNetworks_ClusterAPIVM_vnet_name": { - "value": "ClusterAPIVnet" - }, - "virtualMachines_ClusterAPIVM_name": { - "value": "ClusterAPIVM" - }, - "networkInterfaces_ClusterAPI_name": { - "value": "ClusterAPINIC" - }, - "publicIPAddresses_ClusterAPI_ip_name": { - "value": "ClusterAPIIP" - }, - "networkSecurityGroups_ClusterAPIVM_nsg_name": { - "value": "ClusterAPINSG" - }, - "subnets_default_name": { - "value": "ClusterAPISubnet" - }, - "osDisk_name": { - "value": "_OsDisk_1_2e3ae1ad37414eaca81b432401fcdd75" - }, - "vm_user": { - "value": "ClusterAPI" - }, - "vm_password": { - "value": "_" - }, - "vm_size": { - "value": "Standard_B2ms" - }, - "location": { - "value": "eastus" - }, - "os_type": { - "value": "Linux" - } -} diff --git a/pkg/cloud/azure/services/resources/template/deployment-template.json b/pkg/cloud/azure/services/resources/template/deployment-template.json index 9884130f79d..2de76f121b3 100644 --- a/pkg/cloud/azure/services/resources/template/deployment-template.json +++ b/pkg/cloud/azure/services/resources/template/deployment-template.json @@ -93,6 +93,9 @@ }, "apiVersion": "2017-03-30", "location": "[parameters('location')]", + "zones": [ + "3" + ], "scale": null, "properties": { "hardwareProfile": { @@ -204,6 +207,9 @@ }, "apiVersion": "2017-06-01", "location": "[parameters('location')]", + "zones": [ + "3" + ], "scale": null, "properties": { "provisioningState": "Succeeded", diff --git a/pkg/deployer/deployer.go b/pkg/deployer/deployer.go index 9e708e7d19c..35423dd250b 100644 --- a/pkg/deployer/deployer.go +++ b/pkg/deployer/deployer.go @@ -58,32 +58,19 @@ func (d *Deployer) GetIP(cluster *clusterv1.Cluster, machine *clusterv1.Machine) return scope.ClusterStatus.Network.APIServerIP.IPAddress, nil } - pipsvc := network.NewService(scope) + pipSvc := network.NewService(scope) + // TODO: Remove once resourcesSvc.GetPublicIPName() has moved to network package. + resourcesSvc := resources.NewService(scope) - pip, err := pipsvc.GetPublicIPAddress(scope.ClusterConfig.ResourceGroup, resources.GetPublicIPName(machine)) + pip, err := pipSvc.CreateOrUpdatePublicIPAddress(scope.ClusterConfig.ResourceGroup, resourcesSvc.GetPublicIPName(machine)) if err != nil { return "", err } return *pip.IPAddress, nil } -/* -// GetIP returns the ip address of an existing machine based on the cluster and machine spec passed. -func (a *Actuator) GetIP(cluster *clusterv1.Cluster, machine *clusterv1.Machine) (string, error) { - clusterConfig, err := clusterProviderFromProviderSpec(cluster.Spec.ProviderSpec) - if err != nil { - return "", fmt.Errorf("error loading cluster provider config: %v", err) - } - publicIP, err := azure.services.Network.GetPublicIPAddress(clusterConfig.ResourceGroup, resources.GetPublicIPName(machine)) - if err != nil { - return "", fmt.Errorf("error getting public ip address: %v", err) - } - return *publicIP.IPAddress, nil -} -*/ - // GetKubeConfig returns the kubeconfig after the bootstrap process is complete. -func (d *Deployer) GetKubeConfig(cluster *clusterv1.Cluster, _ *clusterv1.Machine) (string, error) { +func (d *Deployer) GetKubeConfig(cluster *clusterv1.Cluster, machine *clusterv1.Machine) (string, error) { // Load provider config. config, err := providerv1.ClusterConfigFromProviderSpec(cluster.Spec.ProviderSpec) @@ -91,23 +78,28 @@ func (d *Deployer) GetKubeConfig(cluster *clusterv1.Cluster, _ *clusterv1.Machin return "", errors.Errorf("failed to load cluster provider status: %v", err) } - cert, err := certificates.DecodeCertPEM(config.CACertificate) + cert, err := certificates.DecodeCertPEM(config.CAKeyPair.Cert) if err != nil { return "", errors.Wrap(err, "failed to decode CA Cert") } else if cert == nil { return "", errors.New("certificate not found in config") } - key, err := certificates.DecodePrivateKeyPEM(config.CAPrivateKey) + key, err := certificates.DecodePrivateKeyPEM(config.CAKeyPair.Key) if err != nil { return "", errors.Wrap(err, "failed to decode private key") } else if key == nil { return "", errors.New("key not found in status") } - dnsName, err := d.GetIP(cluster, nil) - if err != nil { - return "", errors.Wrap(err, "failed to get DNS address") + // TODO: Unwrap this once load balancer is implemented + dnsName := "null" + + if machine != nil { + dnsName, err = d.GetIP(cluster, nil) + if err != nil { + return "", errors.Wrap(err, "failed to get DNS address") + } } server := fmt.Sprintf("https://%s:6443", dnsName) diff --git a/pkg/deployer/deployer_test.go b/pkg/deployer/deployer_test.go index 3520fe37450..7b3ba401a6d 100644 --- a/pkg/deployer/deployer_test.go +++ b/pkg/deployer/deployer_test.go @@ -16,6 +16,7 @@ limitations under the License. package deployer_test +// TODO: Rewrite deployer tests /* import ( "testing" diff --git a/pkg/tokens/BUILD.bazel b/pkg/tokens/BUILD.bazel new file mode 100644 index 00000000000..1bb18a00a0b --- /dev/null +++ b/pkg/tokens/BUILD.bazel @@ -0,0 +1,17 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["tokens.go"], + importpath = "sigs.k8s.io/cluster-api-provider-azure/pkg/tokens", + visibility = ["//visibility:public"], + deps = [ + "//vendor/github.com/pkg/errors:go_default_library", + "//vendor/k8s.io/api/core/v1:go_default_library", + "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", + "//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library", + "//vendor/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library", + "//vendor/k8s.io/cluster-bootstrap/token/api:go_default_library", + "//vendor/k8s.io/cluster-bootstrap/token/util:go_default_library", + ], +) diff --git a/pkg/tokens/tokens.go b/pkg/tokens/tokens.go new file mode 100644 index 00000000000..98c27863f9d --- /dev/null +++ b/pkg/tokens/tokens.go @@ -0,0 +1,94 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package tokens + +import ( + "time" + + "github.com/pkg/errors" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/wait" + corev1 "k8s.io/client-go/kubernetes/typed/core/v1" + bootstrapapi "k8s.io/cluster-bootstrap/token/api" + bootstraputil "k8s.io/cluster-bootstrap/token/util" +) + +var ( + MaximumRetries = 5 +) + +// NewBootstrap attempts to create a token with the given ID. +func NewBootstrap(client corev1.SecretsGetter, ttl time.Duration) (string, error) { + token, err := bootstraputil.GenerateBootstrapToken() + if err != nil { + return "", errors.Wrap(err, "unable to generate bootstrap token") + } + + substrs := bootstraputil.BootstrapTokenRegexp.FindStringSubmatch(token) + if len(substrs) != 3 { + return "", errors.Wrapf(err, "the bootstrap token %q was not of the form %q", token, bootstrapapi.BootstrapTokenPattern) + } + tokenID := substrs[1] + tokenSecret := substrs[2] + + secretName := bootstraputil.BootstrapTokenSecretName(tokenID) + secretToken := &v1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: secretName, + Namespace: metav1.NamespaceSystem, + }, + Type: bootstrapapi.SecretTypeBootstrapToken, + Data: map[string][]byte{ + bootstrapapi.BootstrapTokenIDKey: []byte(tokenID), + bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret), + bootstrapapi.BootstrapTokenExpirationKey: []byte(time.Now().UTC().Add(ttl).Format(time.RFC3339)), + bootstrapapi.BootstrapTokenUsageSigningKey: []byte("true"), + bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"), + bootstrapapi.BootstrapTokenExtraGroupsKey: []byte("system:bootstrappers:kubeadm:default-node-token"), + }, + } + + err = TryRunCommand(func() error { + _, err := client.Secrets(secretToken.ObjectMeta.Namespace).Create(secretToken) + return err + }, MaximumRetries) + + if err != nil { + return "", errors.Wrap(err, "unable to create secret") + } + + return token, nil +} + +// TryRunCommand runs a function a maximum of failureThreshold times, and retries on error. If failureThreshold is hit; the last error is returned +func TryRunCommand(f func() error, failureThreshold int) error { + backoff := wait.Backoff{ + Duration: 5 * time.Second, + Factor: 2, // double the timeout for every failure + Steps: failureThreshold, + } + return wait.ExponentialBackoff(backoff, func() (bool, error) { + err := f() + if err != nil { + // Retry until the timeout + return false, nil + } + // The last f() call was a success, return cleanly + return true, nil + }) +} diff --git a/test/e2e/BUILD.bazel b/test/e2e/BUILD.bazel index 5c7013151a6..ce9fdfe09ca 100644 --- a/test/e2e/BUILD.bazel +++ b/test/e2e/BUILD.bazel @@ -9,11 +9,6 @@ go_library( importpath = "sigs.k8s.io/cluster-api-provider-azure/test/e2e", visibility = ["//visibility:public"], deps = [ - "//pkg/cloud/azure/actuators:go_default_library", - "//pkg/cloud/azure/services/compute:go_default_library", - "//pkg/cloud/azure/services/network:go_default_library", - "//pkg/cloud/azure/services/resources:go_default_library", - "//vendor/github.com/Azure/go-autorest/autorest/azure/auth:go_default_library", "//vendor/k8s.io/api/core/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/client-go/kubernetes:go_default_library", diff --git a/test/e2e/azure.go b/test/e2e/azure.go index 2116275c243..d232bd2bfdb 100644 --- a/test/e2e/azure.go +++ b/test/e2e/azure.go @@ -16,6 +16,8 @@ limitations under the License. package e2e +// TODO: Rewrite e2e tests. Old tests are effectively useless (and failing) with the refactor (#68) +/* import ( "github.com/Azure/go-autorest/autorest/azure/auth" "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/actuators" @@ -43,3 +45,4 @@ func NewAzureServicesClient(subscriptionID string) (*actuators.AzureClients, err Network: azureNetworkClient, }, nil } +*/ diff --git a/test/e2e/kube.go b/test/e2e/kube.go index 63383b1b1d3..5e774d7798b 100644 --- a/test/e2e/kube.go +++ b/test/e2e/kube.go @@ -40,7 +40,7 @@ func NewKubeClient(kubeconfig string) (*KubeClient, error) { if err != nil { return nil, fmt.Errorf("error creating core clientset: %v", err) } - clusterapiClientset, err := clientcmd.NewClusterApiClientForDefaultSearchPath(kubeconfig, clientcmd.NewConfigOverrides()) + clusterapiClientset, err := clientcmd.NewClusterAPIClientForDefaultSearchPath(kubeconfig, clientcmd.NewConfigOverrides()) if err != nil { return nil, fmt.Errorf("error creating rest config: %v", err) } diff --git a/vendor/github.com/emicklei/go-restful/CHANGES.md b/vendor/github.com/emicklei/go-restful/CHANGES.md index 195449fd15e..5597fa1cc4f 100644 --- a/vendor/github.com/emicklei/go-restful/CHANGES.md +++ b/vendor/github.com/emicklei/go-restful/CHANGES.md @@ -1,5 +1,9 @@ Change history of go-restful = + +v2.9.0 +- add per Route content encoding setting (overrides container setting) + v2.8.0 - add Request.QueryParameters() - add json-iterator (via build tag) diff --git a/vendor/github.com/emicklei/go-restful/container.go b/vendor/github.com/emicklei/go-restful/container.go index b4ad153e8db..061a8d7189b 100644 --- a/vendor/github.com/emicklei/go-restful/container.go +++ b/vendor/github.com/emicklei/go-restful/container.go @@ -97,7 +97,7 @@ func (c *Container) Add(service *WebService) *Container { // cannot have duplicate root paths for _, each := range c.webServices { if each.RootPath() == service.RootPath() { - log.Printf("[restful] WebService with duplicate root path detected:['%v']", each) + log.Printf("WebService with duplicate root path detected:['%v']", each) os.Exit(1) } } @@ -139,7 +139,7 @@ func (c *Container) addHandler(service *WebService, serveMux *http.ServeMux) boo func (c *Container) Remove(ws *WebService) error { if c.ServeMux == http.DefaultServeMux { - errMsg := fmt.Sprintf("[restful] cannot remove a WebService from a Container using the DefaultServeMux: ['%v']", ws) + errMsg := fmt.Sprintf("cannot remove a WebService from a Container using the DefaultServeMux: ['%v']", ws) log.Print(errMsg) return errors.New(errMsg) } @@ -168,7 +168,7 @@ func (c *Container) Remove(ws *WebService) error { // This may be a security issue as it exposes sourcecode information. func logStackOnRecover(panicReason interface{}, httpWriter http.ResponseWriter) { var buffer bytes.Buffer - buffer.WriteString(fmt.Sprintf("[restful] recover from panic situation: - %v\r\n", panicReason)) + buffer.WriteString(fmt.Sprintf("recover from panic situation: - %v\r\n", panicReason)) for i := 2; ; i += 1 { _, file, line, ok := runtime.Caller(i) if !ok { @@ -220,31 +220,37 @@ func (c *Container) dispatch(httpWriter http.ResponseWriter, httpRequest *http.R }() } + // Find best match Route ; err is non nil if no match was found + var webService *WebService + var route *Route + var err error + func() { + c.webServicesLock.RLock() + defer c.webServicesLock.RUnlock() + webService, route, err = c.router.SelectRoute( + c.webServices, + httpRequest) + }() + // Detect if compression is needed // assume without compression, test for override - if c.contentEncodingEnabled { + contentEncodingEnabled := c.contentEncodingEnabled + if route != nil && route.contentEncodingEnabled != nil { + contentEncodingEnabled = *route.contentEncodingEnabled + } + if contentEncodingEnabled { doCompress, encoding := wantsCompressedResponse(httpRequest) if doCompress { var err error writer, err = NewCompressingResponseWriter(httpWriter, encoding) if err != nil { - log.Print("[restful] unable to install compressor: ", err) + log.Print("unable to install compressor: ", err) httpWriter.WriteHeader(http.StatusInternalServerError) return } } } - // Find best match Route ; err is non nil if no match was found - var webService *WebService - var route *Route - var err error - func() { - c.webServicesLock.RLock() - defer c.webServicesLock.RUnlock() - webService, route, err = c.router.SelectRoute( - c.webServices, - httpRequest) - }() + if err != nil { // a non-200 response has already been written // run container filters anyway ; they should not touch the response... diff --git a/vendor/github.com/emicklei/go-restful/jsr311.go b/vendor/github.com/emicklei/go-restful/jsr311.go index 4360b492ec1..bc303ef7093 100644 --- a/vendor/github.com/emicklei/go-restful/jsr311.go +++ b/vendor/github.com/emicklei/go-restful/jsr311.go @@ -99,11 +99,10 @@ func (r RouterJSR311) detectRoute(routes []Route, httpRequest *http.Request) (*R } return nil, NewError(http.StatusMethodNotAllowed, "405: Method Not Allowed") } - inputMediaOk := methodOk // content-type contentType := httpRequest.Header.Get(HEADER_ContentType) - inputMediaOk = []Route{} + inputMediaOk := []Route{} for _, each := range methodOk { if each.matchesContentType(contentType) { inputMediaOk = append(inputMediaOk, each) diff --git a/vendor/github.com/emicklei/go-restful/route.go b/vendor/github.com/emicklei/go-restful/route.go index f72bf985079..592638ab602 100644 --- a/vendor/github.com/emicklei/go-restful/route.go +++ b/vendor/github.com/emicklei/go-restful/route.go @@ -45,6 +45,9 @@ type Route struct { // marks a route as deprecated Deprecated bool + + //Overrides the container.contentEncodingEnabled + contentEncodingEnabled *bool } // Initialize for Route @@ -147,3 +150,8 @@ func tokenizePath(path string) []string { func (r Route) String() string { return r.Method + " " + r.Path } + +// EnableContentEncoding (default=false) allows for GZIP or DEFLATE encoding of responses. Overrides the container.contentEncodingEnabled value. +func (r Route) EnableContentEncoding(enabled bool) { + r.contentEncodingEnabled = &enabled +} diff --git a/vendor/github.com/emicklei/go-restful/route_builder.go b/vendor/github.com/emicklei/go-restful/route_builder.go index 4ebecbd8c41..a4e74d10b8e 100644 --- a/vendor/github.com/emicklei/go-restful/route_builder.go +++ b/vendor/github.com/emicklei/go-restful/route_builder.go @@ -261,11 +261,11 @@ func (b *RouteBuilder) typeNameHandler(handler TypeNameHandleFunction) *RouteBui func (b *RouteBuilder) Build() Route { pathExpr, err := newPathExpression(b.currentPath) if err != nil { - log.Printf("[restful] Invalid path:%s because:%v", b.currentPath, err) + log.Printf("Invalid path:%s because:%v", b.currentPath, err) os.Exit(1) } if b.function == nil { - log.Printf("[restful] No function specified for route:" + b.currentPath) + log.Printf("No function specified for route:" + b.currentPath) os.Exit(1) } operationName := b.operation diff --git a/vendor/github.com/emicklei/go-restful/web_service.go b/vendor/github.com/emicklei/go-restful/web_service.go index f7e18a5859c..77ba9a8cfc5 100644 --- a/vendor/github.com/emicklei/go-restful/web_service.go +++ b/vendor/github.com/emicklei/go-restful/web_service.go @@ -60,7 +60,7 @@ func reflectTypeName(sample interface{}) string { func (w *WebService) compilePathExpression() { compiled, err := newPathExpression(w.rootPath) if err != nil { - log.Printf("[restful] invalid path:%s because:%v", w.rootPath, err) + log.Printf("invalid path:%s because:%v", w.rootPath, err) os.Exit(1) } w.pathExpr = compiled diff --git a/vendor/github.com/gobuffalo/envy/.env b/vendor/github.com/gobuffalo/envy/.env deleted file mode 100644 index 33eeb3b13b9..00000000000 --- a/vendor/github.com/gobuffalo/envy/.env +++ /dev/null @@ -1,5 +0,0 @@ -# This is a comment -# We can use equal or colon notation -DIR: root -FLAVOUR: none -INSIDE_FOLDER=false \ No newline at end of file diff --git a/vendor/github.com/gobuffalo/envy/.travis.yml b/vendor/github.com/gobuffalo/envy/.travis.yml index aa98da4fbc5..1fb041a2592 100644 --- a/vendor/github.com/gobuffalo/envy/.travis.yml +++ b/vendor/github.com/gobuffalo/envy/.travis.yml @@ -29,6 +29,8 @@ matrix: env: - GO111MODULE=on -install: make deps +install: false -script: make ci-test +script: + - go get -v -t ./... + - go test -v -timeout=5s -race ./... diff --git a/vendor/github.com/gobuffalo/envy/BUILD.bazel b/vendor/github.com/gobuffalo/envy/BUILD.bazel index c0e5df52940..ab8bdf01a99 100644 --- a/vendor/github.com/gobuffalo/envy/BUILD.bazel +++ b/vendor/github.com/gobuffalo/envy/BUILD.bazel @@ -9,5 +9,8 @@ go_library( importmap = "sigs.k8s.io/cluster-api-provider-azure/vendor/github.com/gobuffalo/envy", importpath = "github.com/gobuffalo/envy", visibility = ["//visibility:public"], - deps = ["//vendor/github.com/joho/godotenv:go_default_library"], + deps = [ + "//vendor/github.com/joho/godotenv:go_default_library", + "//vendor/github.com/rogpeppe/go-internal/modfile:go_default_library", + ], ) diff --git a/vendor/github.com/gobuffalo/envy/Makefile b/vendor/github.com/gobuffalo/envy/Makefile index b0db1c4f887..46aece8ff32 100644 --- a/vendor/github.com/gobuffalo/envy/Makefile +++ b/vendor/github.com/gobuffalo/envy/Makefile @@ -2,23 +2,23 @@ TAGS ?= "sqlite" GO_BIN ?= go install: - packr + packr2 $(GO_BIN) install -v . deps: $(GO_BIN) get github.com/gobuffalo/release - $(GO_BIN) get github.com/gobuffalo/packr/packr + $(GO_BIN) get github.com/gobuffalo/packr/v2/packr2 $(GO_BIN) get -tags ${TAGS} -t ./... ifeq ($(GO111MODULE),on) $(GO_BIN) mod tidy endif build: - packr + packr2 $(GO_BIN) build -v . test: - packr + packr2 $(GO_BIN) test -tags ${TAGS} ./... ci-test: @@ -32,7 +32,7 @@ update: ifeq ($(GO111MODULE),on) $(GO_BIN) mod tidy endif - packr + packr2 make test make install ifeq ($(GO111MODULE),on) diff --git a/vendor/github.com/gobuffalo/envy/SHOULDERS.md b/vendor/github.com/gobuffalo/envy/SHOULDERS.md new file mode 100644 index 00000000000..6c9cd7bdfe1 --- /dev/null +++ b/vendor/github.com/gobuffalo/envy/SHOULDERS.md @@ -0,0 +1,16 @@ +# `github.com/gobuffalo/envy` Stands on the Shoulders of Giants + +`github.com/gobuffalo/envy` does not try to reinvent the wheel! Instead, it uses the already great wheels developed by the Go community and puts them all together in the best way possible. Without these giants this project would not be possible. Please make sure to check them out and thank them for all of their hard work. + +Thank you to the following **GIANTS**: + + +* [github.com/gobuffalo/envy](https://godoc.org/github.com/gobuffalo/envy) + +* [github.com/joho/godotenv](https://godoc.org/github.com/joho/godotenv) + +* [github.com/rogpeppe/go-internal/modfile](https://godoc.org/github.com/rogpeppe/go-internal/modfile) + +* [github.com/rogpeppe/go-internal/module](https://godoc.org/github.com/rogpeppe/go-internal/module) + +* [github.com/rogpeppe/go-internal/semver](https://godoc.org/github.com/rogpeppe/go-internal/semver) diff --git a/vendor/github.com/gobuffalo/envy/envy.go b/vendor/github.com/gobuffalo/envy/envy.go index 9d43448b8d4..d3995f45577 100644 --- a/vendor/github.com/gobuffalo/envy/envy.go +++ b/vendor/github.com/gobuffalo/envy/envy.go @@ -11,8 +11,10 @@ package envy makes working with ENV variables in Go trivial. package envy import ( + "errors" "flag" "fmt" + "io/ioutil" "os" "os/exec" "path/filepath" @@ -21,6 +23,7 @@ import ( "sync" "github.com/joho/godotenv" + "github.com/rogpeppe/go-internal/modfile" ) var gil = &sync.RWMutex{} @@ -192,6 +195,16 @@ func GoBin() string { return Get("GO_BIN", "go") } +func InGoPath() bool { + pwd, _ := os.Getwd() + for _, p := range GoPaths() { + if strings.HasPrefix(pwd, p) { + return true + } + } + return false +} + // GoPaths returns all possible GOPATHS that are set. func GoPaths() []string { gp := Get("GOPATH", "") @@ -202,6 +215,7 @@ func GoPaths() []string { } func importPath(path string) string { + path = strings.TrimPrefix(path, "/private") for _, gopath := range GoPaths() { srcpath := filepath.Join(gopath, "src") rel, err := filepath.Rel(srcpath, path) @@ -216,7 +230,29 @@ func importPath(path string) string { return filepath.ToSlash(rel) } +// CurrentModule will attempt to return the module name from `go.mod` if +// modules are enabled. +// If modules are not enabled it will fallback to using CurrentPackage instead. +func CurrentModule() (string, error) { + if !Mods() { + return CurrentPackage(), nil + } + moddata, err := ioutil.ReadFile("go.mod") + if err != nil { + return "", errors.New("go.mod cannot be read or does not exist while go module is enabled") + } + packagePath := modfile.ModulePath(moddata) + if packagePath == "" { + return "", errors.New("go.mod is malformed") + } + return packagePath, nil +} + +// CurrentPackage attempts to figure out the current package name from the PWD +// Use CurrentModule for a more accurate package name. func CurrentPackage() string { + if Mods() { + } pwd, _ := os.Getwd() return importPath(pwd) } diff --git a/vendor/github.com/gobuffalo/envy/go.mod b/vendor/github.com/gobuffalo/envy/go.mod index 149e25a4590..d951b7ce1c2 100644 --- a/vendor/github.com/gobuffalo/envy/go.mod +++ b/vendor/github.com/gobuffalo/envy/go.mod @@ -3,6 +3,6 @@ module github.com/gobuffalo/envy require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/joho/godotenv v1.3.0 - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/testify v1.2.2 + github.com/rogpeppe/go-internal v1.1.0 + github.com/stretchr/testify v1.3.0 ) diff --git a/vendor/github.com/gobuffalo/envy/go.sum b/vendor/github.com/gobuffalo/envy/go.sum index 868dbe8d81e..f11ef4ce58e 100644 --- a/vendor/github.com/gobuffalo/envy/go.sum +++ b/vendor/github.com/gobuffalo/envy/go.sum @@ -1,8 +1,17 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/rogpeppe/go-internal v1.1.0 h1:g0fH8RicVgNl+zVZDCDfbdWxAWoAEJyI7I3TZYXFiig= +github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= diff --git a/vendor/github.com/gobuffalo/envy/shoulders.md b/vendor/github.com/gobuffalo/envy/shoulders.md deleted file mode 100644 index 0b22abc2a13..00000000000 --- a/vendor/github.com/gobuffalo/envy/shoulders.md +++ /dev/null @@ -1,10 +0,0 @@ -# github.com/gobuffalo/envy Stands on the Shoulders of Giants - -github.com/gobuffalo/envy does not try to reinvent the wheel! Instead, it uses the already great wheels developed by the Go community and puts them all together in the best way possible. Without these giants this project would not be possible. Please make sure to check them out and thank them for all of their hard work. - -Thank you to the following **GIANTS**: - - -* [github.com/gobuffalo/envy](https://godoc.org/github.com/gobuffalo/envy) - -* [github.com/joho/godotenv](https://godoc.org/github.com/joho/godotenv) diff --git a/vendor/github.com/gobuffalo/envy/version.go b/vendor/github.com/gobuffalo/envy/version.go index f41fdc0746c..0bff116a5db 100644 --- a/vendor/github.com/gobuffalo/envy/version.go +++ b/vendor/github.com/gobuffalo/envy/version.go @@ -1,3 +1,3 @@ package envy -const Version = "v1.6.7" +const Version = "v1.6.15" diff --git a/vendor/github.com/gogo/protobuf/LICENSE b/vendor/github.com/gogo/protobuf/LICENSE index 7be0cc7b62c..f57de90da8a 100644 --- a/vendor/github.com/gogo/protobuf/LICENSE +++ b/vendor/github.com/gogo/protobuf/LICENSE @@ -1,7 +1,6 @@ -Protocol Buffers for Go with Gadgets - Copyright (c) 2013, The GoGo Authors. All rights reserved. -http://github.com/gogo/protobuf + +Protocol Buffers for Go with Gadgets Go support for Protocol Buffers - Google's data interchange format diff --git a/vendor/github.com/gogo/protobuf/proto/BUILD.bazel b/vendor/github.com/gogo/protobuf/proto/BUILD.bazel index 77690f64d4a..3b827d76482 100644 --- a/vendor/github.com/gogo/protobuf/proto/BUILD.bazel +++ b/vendor/github.com/gogo/protobuf/proto/BUILD.bazel @@ -32,6 +32,8 @@ go_library( "text_parser.go", "timestamp.go", "timestamp_gogo.go", + "wrappers.go", + "wrappers_gogo.go", ], importmap = "sigs.k8s.io/cluster-api-provider-azure/vendor/github.com/gogo/protobuf/proto", importpath = "github.com/gogo/protobuf/proto", diff --git a/vendor/github.com/gogo/protobuf/proto/encode.go b/vendor/github.com/gogo/protobuf/proto/encode.go index c27d35f866b..3abfed2cff0 100644 --- a/vendor/github.com/gogo/protobuf/proto/encode.go +++ b/vendor/github.com/gogo/protobuf/proto/encode.go @@ -37,27 +37,9 @@ package proto import ( "errors" - "fmt" "reflect" ) -// RequiredNotSetError is the error returned if Marshal is called with -// a protocol buffer struct whose required fields have not -// all been initialized. It is also the error returned if Unmarshal is -// called with an encoded protocol buffer that does not include all the -// required fields. -// -// When printed, RequiredNotSetError reports the first unset required field in a -// message. If the field cannot be precisely determined, it is reported as -// "{Unknown}". -type RequiredNotSetError struct { - field string -} - -func (e *RequiredNotSetError) Error() string { - return fmt.Sprintf("proto: required field %q not set", e.field) -} - var ( // errRepeatedHasNil is the error returned if Marshal is called with // a struct with a repeated field containing a nil element. diff --git a/vendor/github.com/gogo/protobuf/proto/lib.go b/vendor/github.com/gogo/protobuf/proto/lib.go index 0f1950c67e4..b2271d0b7b8 100644 --- a/vendor/github.com/gogo/protobuf/proto/lib.go +++ b/vendor/github.com/gogo/protobuf/proto/lib.go @@ -265,7 +265,6 @@ package proto import ( "encoding/json" - "errors" "fmt" "log" "reflect" @@ -274,7 +273,66 @@ import ( "sync" ) -var errInvalidUTF8 = errors.New("proto: invalid UTF-8 string") +// RequiredNotSetError is an error type returned by either Marshal or Unmarshal. +// Marshal reports this when a required field is not initialized. +// Unmarshal reports this when a required field is missing from the wire data. +type RequiredNotSetError struct{ field string } + +func (e *RequiredNotSetError) Error() string { + if e.field == "" { + return fmt.Sprintf("proto: required field not set") + } + return fmt.Sprintf("proto: required field %q not set", e.field) +} +func (e *RequiredNotSetError) RequiredNotSet() bool { + return true +} + +type invalidUTF8Error struct{ field string } + +func (e *invalidUTF8Error) Error() string { + if e.field == "" { + return "proto: invalid UTF-8 detected" + } + return fmt.Sprintf("proto: field %q contains invalid UTF-8", e.field) +} +func (e *invalidUTF8Error) InvalidUTF8() bool { + return true +} + +// errInvalidUTF8 is a sentinel error to identify fields with invalid UTF-8. +// This error should not be exposed to the external API as such errors should +// be recreated with the field information. +var errInvalidUTF8 = &invalidUTF8Error{} + +// isNonFatal reports whether the error is either a RequiredNotSet error +// or a InvalidUTF8 error. +func isNonFatal(err error) bool { + if re, ok := err.(interface{ RequiredNotSet() bool }); ok && re.RequiredNotSet() { + return true + } + if re, ok := err.(interface{ InvalidUTF8() bool }); ok && re.InvalidUTF8() { + return true + } + return false +} + +type nonFatal struct{ E error } + +// Merge merges err into nf and reports whether it was successful. +// Otherwise it returns false for any fatal non-nil errors. +func (nf *nonFatal) Merge(err error) (ok bool) { + if err == nil { + return true // not an error + } + if !isNonFatal(err) { + return false // fatal error + } + if nf.E == nil { + nf.E = err // store first instance of non-fatal error + } + return true +} // Message is implemented by generated protocol buffer messages. type Message interface { @@ -570,9 +628,11 @@ func SetDefaults(pb Message) { setDefaults(reflect.ValueOf(pb), true, false) } -// v is a pointer to a struct. +// v is a struct. func setDefaults(v reflect.Value, recur, zeros bool) { - v = v.Elem() + if v.Kind() == reflect.Ptr { + v = v.Elem() + } defaultMu.RLock() dm, ok := defaults[v.Type()] @@ -674,8 +734,11 @@ func setDefaults(v reflect.Value, recur, zeros bool) { for _, ni := range dm.nested { f := v.Field(ni) - // f is *T or []*T or map[T]*T + // f is *T or T or []*T or []T switch f.Kind() { + case reflect.Struct: + setDefaults(f, recur, zeros) + case reflect.Ptr: if f.IsNil() { continue @@ -685,7 +748,7 @@ func setDefaults(v reflect.Value, recur, zeros bool) { case reflect.Slice: for i := 0; i < f.Len(); i++ { e := f.Index(i) - if e.IsNil() { + if e.Kind() == reflect.Ptr && e.IsNil() { continue } setDefaults(e, recur, zeros) @@ -757,6 +820,9 @@ func buildDefaultMessage(t reflect.Type) (dm defaultMessage) { func fieldDefault(ft reflect.Type, prop *Properties) (sf *scalarField, nestedMessage bool, err error) { var canHaveDefault bool switch ft.Kind() { + case reflect.Struct: + nestedMessage = true // non-nullable + case reflect.Ptr: if ft.Elem().Kind() == reflect.Struct { nestedMessage = true @@ -766,7 +832,7 @@ func fieldDefault(ft reflect.Type, prop *Properties) (sf *scalarField, nestedMes case reflect.Slice: switch ft.Elem().Kind() { - case reflect.Ptr: + case reflect.Ptr, reflect.Struct: nestedMessage = true // repeated message case reflect.Uint8: canHaveDefault = true // bytes field diff --git a/vendor/github.com/gogo/protobuf/proto/pointer_unsafe_gogo.go b/vendor/github.com/gogo/protobuf/proto/pointer_unsafe_gogo.go index b354101b9c3..aca8eed02a1 100644 --- a/vendor/github.com/gogo/protobuf/proto/pointer_unsafe_gogo.go +++ b/vendor/github.com/gogo/protobuf/proto/pointer_unsafe_gogo.go @@ -26,7 +26,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// +build !purego !appengine,!js +// +build !purego,!appengine,!js // This file contains the implementation of the proto field accesses using package unsafe. diff --git a/vendor/github.com/gogo/protobuf/proto/properties.go b/vendor/github.com/gogo/protobuf/proto/properties.go index 7a5e28efe51..04dcb8d9ef2 100644 --- a/vendor/github.com/gogo/protobuf/proto/properties.go +++ b/vendor/github.com/gogo/protobuf/proto/properties.go @@ -144,7 +144,7 @@ type Properties struct { Repeated bool Packed bool // relevant for repeated primitives only Enum string // set for enum types only - proto3 bool // whether this is known to be a proto3 field; set for []byte only + proto3 bool // whether this is known to be a proto3 field oneof bool // whether this is a oneof field Default string // default value @@ -153,14 +153,15 @@ type Properties struct { CastType string StdTime bool StdDuration bool + WktPointer bool stype reflect.Type // set for struct types only ctype reflect.Type // set for custom types only sprop *StructProperties // set for struct types only - mtype reflect.Type // set for map types only - mkeyprop *Properties // set for map types only - mvalprop *Properties // set for map types only + mtype reflect.Type // set for map types only + MapKeyProp *Properties // set for map types only + MapValProp *Properties // set for map types only } // String formats the properties in the protobuf struct field tag style. @@ -274,6 +275,8 @@ outer: p.StdTime = true case f == "stdduration": p.StdDuration = true + case f == "wktptr": + p.WktPointer = true } } } @@ -296,6 +299,10 @@ func (p *Properties) setFieldProps(typ reflect.Type, f *reflect.StructField, loc p.setTag(lockGetProp) return } + if p.WktPointer && !isMap { + p.setTag(lockGetProp) + return + } switch t1 := typ; t1.Kind() { case reflect.Struct: p.stype = typ @@ -317,9 +324,9 @@ func (p *Properties) setFieldProps(typ reflect.Type, f *reflect.StructField, loc case reflect.Map: p.mtype = t1 - p.mkeyprop = &Properties{} - p.mkeyprop.init(reflect.PtrTo(p.mtype.Key()), "Key", f.Tag.Get("protobuf_key"), nil, lockGetProp) - p.mvalprop = &Properties{} + p.MapKeyProp = &Properties{} + p.MapKeyProp.init(reflect.PtrTo(p.mtype.Key()), "Key", f.Tag.Get("protobuf_key"), nil, lockGetProp) + p.MapValProp = &Properties{} vtype := p.mtype.Elem() if vtype.Kind() != reflect.Ptr && vtype.Kind() != reflect.Slice { // The value type is not a message (*T) or bytes ([]byte), @@ -327,10 +334,11 @@ func (p *Properties) setFieldProps(typ reflect.Type, f *reflect.StructField, loc vtype = reflect.PtrTo(vtype) } - p.mvalprop.CustomType = p.CustomType - p.mvalprop.StdDuration = p.StdDuration - p.mvalprop.StdTime = p.StdTime - p.mvalprop.init(vtype, "Value", f.Tag.Get("protobuf_val"), nil, lockGetProp) + p.MapValProp.CustomType = p.CustomType + p.MapValProp.StdDuration = p.StdDuration + p.MapValProp.StdTime = p.StdTime + p.MapValProp.WktPointer = p.WktPointer + p.MapValProp.init(vtype, "Value", f.Tag.Get("protobuf_val"), nil, lockGetProp) } p.setTag(lockGetProp) } diff --git a/vendor/github.com/gogo/protobuf/proto/table_marshal.go b/vendor/github.com/gogo/protobuf/proto/table_marshal.go index 255e7b50886..ba58c49a43f 100644 --- a/vendor/github.com/gogo/protobuf/proto/table_marshal.go +++ b/vendor/github.com/gogo/protobuf/proto/table_marshal.go @@ -97,6 +97,8 @@ type marshalElemInfo struct { var ( marshalInfoMap = map[reflect.Type]*marshalInfo{} marshalInfoLock sync.Mutex + + uint8SliceType = reflect.TypeOf(([]uint8)(nil)).Kind() ) // getMarshalInfo returns the information to marshal a given type of message. @@ -246,16 +248,13 @@ func (u *marshalInfo) marshal(b []byte, ptr pointer, deterministic bool) ([]byte // If the message can marshal itself, let it do it, for compatibility. // NOTE: This is not efficient. if u.hasmarshaler { - if deterministic { - return nil, errors.New("proto: deterministic not supported by the Marshal method of " + u.typ.String()) - } m := ptr.asPointerTo(u.typ).Interface().(Marshaler) b1, err := m.Marshal() b = append(b, b1...) return b, err } - var err, errreq error + var err, errLater error // The old marshaler encodes extensions at beginning. if u.extensions.IsValid() { e := ptr.offset(u.extensions).toExtensions() @@ -280,11 +279,13 @@ func (u *marshalInfo) marshal(b []byte, ptr pointer, deterministic bool) ([]byte b = append(b, s...) } for _, f := range u.fields { - if f.required && errreq == nil { - if ptr.offset(f.field).getPointer().isNil() { + if f.required { + if f.isPointer && ptr.offset(f.field).getPointer().isNil() { // Required field is not set. // We record the error but keep going, to give a complete marshaling. - errreq = &RequiredNotSetError{f.name} + if errLater == nil { + errLater = &RequiredNotSetError{f.name} + } continue } } @@ -297,14 +298,21 @@ func (u *marshalInfo) marshal(b []byte, ptr pointer, deterministic bool) ([]byte if err1, ok := err.(*RequiredNotSetError); ok { // Required field in submessage is not set. // We record the error but keep going, to give a complete marshaling. - if errreq == nil { - errreq = &RequiredNotSetError{f.name + "." + err1.field} + if errLater == nil { + errLater = &RequiredNotSetError{f.name + "." + err1.field} } continue } if err == errRepeatedHasNil { err = errors.New("proto: repeated field " + f.name + " has nil element") } + if err == errInvalidUTF8 { + if errLater == nil { + fullName := revProtoTypes[reflect.PtrTo(u.typ)] + "." + f.name + errLater = &invalidUTF8Error{fullName} + } + continue + } return b, err } } @@ -312,7 +320,7 @@ func (u *marshalInfo) marshal(b []byte, ptr pointer, deterministic bool) ([]byte s := *ptr.offset(u.unrecognized).toBytes() b = append(b, s...) } - return b, errreq + return b, errLater } // computeMarshalInfo initializes the marshal info. @@ -577,6 +585,8 @@ func typeMarshaler(t reflect.Type, tags []string, nozero, oneof bool) (sizer, ma ctype := false isTime := false isDuration := false + isWktPointer := false + validateUTF8 := true for i := 2; i < len(tags); i++ { if tags[i] == "packed" { packed = true @@ -593,7 +603,11 @@ func typeMarshaler(t reflect.Type, tags []string, nozero, oneof bool) (sizer, ma if tags[i] == "stdduration" { isDuration = true } + if tags[i] == "wktptr" { + isWktPointer = true + } } + validateUTF8 = validateUTF8 && proto3 if !proto3 && !pointer && !slice { nozero = false } @@ -638,6 +652,112 @@ func typeMarshaler(t reflect.Type, tags []string, nozero, oneof bool) (sizer, ma return makeDurationMarshaler(getMarshalInfo(t)) } + if isWktPointer { + switch t.Kind() { + case reflect.Float64: + if pointer { + if slice { + return makeStdDoubleValuePtrSliceMarshaler(getMarshalInfo(t)) + } + return makeStdDoubleValuePtrMarshaler(getMarshalInfo(t)) + } + if slice { + return makeStdDoubleValueSliceMarshaler(getMarshalInfo(t)) + } + return makeStdDoubleValueMarshaler(getMarshalInfo(t)) + case reflect.Float32: + if pointer { + if slice { + return makeStdFloatValuePtrSliceMarshaler(getMarshalInfo(t)) + } + return makeStdFloatValuePtrMarshaler(getMarshalInfo(t)) + } + if slice { + return makeStdFloatValueSliceMarshaler(getMarshalInfo(t)) + } + return makeStdFloatValueMarshaler(getMarshalInfo(t)) + case reflect.Int64: + if pointer { + if slice { + return makeStdInt64ValuePtrSliceMarshaler(getMarshalInfo(t)) + } + return makeStdInt64ValuePtrMarshaler(getMarshalInfo(t)) + } + if slice { + return makeStdInt64ValueSliceMarshaler(getMarshalInfo(t)) + } + return makeStdInt64ValueMarshaler(getMarshalInfo(t)) + case reflect.Uint64: + if pointer { + if slice { + return makeStdUInt64ValuePtrSliceMarshaler(getMarshalInfo(t)) + } + return makeStdUInt64ValuePtrMarshaler(getMarshalInfo(t)) + } + if slice { + return makeStdUInt64ValueSliceMarshaler(getMarshalInfo(t)) + } + return makeStdUInt64ValueMarshaler(getMarshalInfo(t)) + case reflect.Int32: + if pointer { + if slice { + return makeStdInt32ValuePtrSliceMarshaler(getMarshalInfo(t)) + } + return makeStdInt32ValuePtrMarshaler(getMarshalInfo(t)) + } + if slice { + return makeStdInt32ValueSliceMarshaler(getMarshalInfo(t)) + } + return makeStdInt32ValueMarshaler(getMarshalInfo(t)) + case reflect.Uint32: + if pointer { + if slice { + return makeStdUInt32ValuePtrSliceMarshaler(getMarshalInfo(t)) + } + return makeStdUInt32ValuePtrMarshaler(getMarshalInfo(t)) + } + if slice { + return makeStdUInt32ValueSliceMarshaler(getMarshalInfo(t)) + } + return makeStdUInt32ValueMarshaler(getMarshalInfo(t)) + case reflect.Bool: + if pointer { + if slice { + return makeStdBoolValuePtrSliceMarshaler(getMarshalInfo(t)) + } + return makeStdBoolValuePtrMarshaler(getMarshalInfo(t)) + } + if slice { + return makeStdBoolValueSliceMarshaler(getMarshalInfo(t)) + } + return makeStdBoolValueMarshaler(getMarshalInfo(t)) + case reflect.String: + if pointer { + if slice { + return makeStdStringValuePtrSliceMarshaler(getMarshalInfo(t)) + } + return makeStdStringValuePtrMarshaler(getMarshalInfo(t)) + } + if slice { + return makeStdStringValueSliceMarshaler(getMarshalInfo(t)) + } + return makeStdStringValueMarshaler(getMarshalInfo(t)) + case uint8SliceType: + if pointer { + if slice { + return makeStdBytesValuePtrSliceMarshaler(getMarshalInfo(t)) + } + return makeStdBytesValuePtrMarshaler(getMarshalInfo(t)) + } + if slice { + return makeStdBytesValueSliceMarshaler(getMarshalInfo(t)) + } + return makeStdBytesValueMarshaler(getMarshalInfo(t)) + default: + panic(fmt.Sprintf("unknown wktpointer type %#v", t)) + } + } + switch t.Kind() { case reflect.Bool: if pointer { @@ -834,6 +954,18 @@ func typeMarshaler(t reflect.Type, tags []string, nozero, oneof bool) (sizer, ma } return sizeFloat64Value, appendFloat64Value case reflect.String: + if validateUTF8 { + if pointer { + return sizeStringPtr, appendUTF8StringPtr + } + if slice { + return sizeStringSlice, appendUTF8StringSlice + } + if nozero { + return sizeStringValueNoZero, appendUTF8StringValueNoZero + } + return sizeStringValue, appendUTF8StringValue + } if pointer { return sizeStringPtr, appendStringPtr } @@ -2089,52 +2221,105 @@ func appendBoolPackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byt return b, nil } func appendStringValue(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) { + v := *ptr.toString() + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(v))) + b = append(b, v...) + return b, nil +} +func appendStringValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) { + v := *ptr.toString() + if v == "" { + return b, nil + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(v))) + b = append(b, v...) + return b, nil +} +func appendStringPtr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) { + p := *ptr.toStringPtr() + if p == nil { + return b, nil + } + v := *p + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(v))) + b = append(b, v...) + return b, nil +} +func appendStringSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) { + s := *ptr.toStringSlice() + for _, v := range s { + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(v))) + b = append(b, v...) + } + return b, nil +} +func appendUTF8StringValue(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) { + var invalidUTF8 bool v := *ptr.toString() if !utf8.ValidString(v) { - return nil, errInvalidUTF8 + invalidUTF8 = true } b = appendVarint(b, wiretag) b = appendVarint(b, uint64(len(v))) b = append(b, v...) + if invalidUTF8 { + return b, errInvalidUTF8 + } return b, nil } -func appendStringValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) { +func appendUTF8StringValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) { + var invalidUTF8 bool v := *ptr.toString() if v == "" { return b, nil } if !utf8.ValidString(v) { - return nil, errInvalidUTF8 + invalidUTF8 = true } b = appendVarint(b, wiretag) b = appendVarint(b, uint64(len(v))) b = append(b, v...) + if invalidUTF8 { + return b, errInvalidUTF8 + } return b, nil } -func appendStringPtr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) { +func appendUTF8StringPtr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) { + var invalidUTF8 bool p := *ptr.toStringPtr() if p == nil { return b, nil } v := *p if !utf8.ValidString(v) { - return nil, errInvalidUTF8 + invalidUTF8 = true } b = appendVarint(b, wiretag) b = appendVarint(b, uint64(len(v))) b = append(b, v...) + if invalidUTF8 { + return b, errInvalidUTF8 + } return b, nil } -func appendStringSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) { +func appendUTF8StringSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) { + var invalidUTF8 bool s := *ptr.toStringSlice() for _, v := range s { if !utf8.ValidString(v) { - return nil, errInvalidUTF8 + invalidUTF8 = true } b = appendVarint(b, wiretag) b = appendVarint(b, uint64(len(v))) b = append(b, v...) } + if invalidUTF8 { + return b, errInvalidUTF8 + } return b, nil } func appendBytes(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) { @@ -2213,7 +2398,8 @@ func makeGroupSliceMarshaler(u *marshalInfo) (sizer, marshaler) { }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { s := ptr.getPointerSlice() - var err, errreq error + var err error + var nerr nonFatal for _, v := range s { if v.isNil() { return b, errRepeatedHasNil @@ -2221,22 +2407,14 @@ func makeGroupSliceMarshaler(u *marshalInfo) (sizer, marshaler) { b = appendVarint(b, wiretag) // start group b, err = u.marshal(b, v, deterministic) b = appendVarint(b, wiretag+(WireEndGroup-WireStartGroup)) // end group - if err != nil { - if _, ok := err.(*RequiredNotSetError); ok { - // Required field in submessage is not set. - // We record the error but keep going, to give a complete marshaling. - if errreq == nil { - errreq = err - } - continue - } + if !nerr.Merge(err) { if err == ErrNil { err = errRepeatedHasNil } return b, err } } - return b, errreq + return b, nerr.E } } @@ -2280,7 +2458,8 @@ func makeMessageSliceMarshaler(u *marshalInfo) (sizer, marshaler) { }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { s := ptr.getPointerSlice() - var err, errreq error + var err error + var nerr nonFatal for _, v := range s { if v.isNil() { return b, errRepeatedHasNil @@ -2289,22 +2468,15 @@ func makeMessageSliceMarshaler(u *marshalInfo) (sizer, marshaler) { siz := u.cachedsize(v) b = appendVarint(b, uint64(siz)) b, err = u.marshal(b, v, deterministic) - if err != nil { - if _, ok := err.(*RequiredNotSetError); ok { - // Required field in submessage is not set. - // We record the error but keep going, to give a complete marshaling. - if errreq == nil { - errreq = err - } - continue - } + + if !nerr.Merge(err) { if err == ErrNil { err = errRepeatedHasNil } return b, err } } - return b, errreq + return b, nerr.E } } @@ -2318,15 +2490,21 @@ func makeMapMarshaler(f *reflect.StructField) (sizer, marshaler) { tags := strings.Split(f.Tag.Get("protobuf"), ",") keyTags := strings.Split(f.Tag.Get("protobuf_key"), ",") valTags := strings.Split(f.Tag.Get("protobuf_val"), ",") + stdOptions := false for _, t := range tags { if strings.HasPrefix(t, "customtype=") { valTags = append(valTags, t) } if t == "stdtime" { valTags = append(valTags, t) + stdOptions = true } if t == "stdduration" { valTags = append(valTags, t) + stdOptions = true + } + if t == "wktptr" { + valTags = append(valTags, t) } } keySizer, keyMarshaler := typeMarshaler(keyType, keyTags, false, false) // don't omit zero value in map @@ -2340,6 +2518,25 @@ func makeMapMarshaler(f *reflect.StructField) (sizer, marshaler) { // value. // Key cannot be pointer-typed. valIsPtr := valType.Kind() == reflect.Ptr + + // If value is a message with nested maps, calling + // valSizer in marshal may be quadratic. We should use + // cached version in marshal (but not in size). + // If value is not message type, we don't have size cache, + // but it cannot be nested either. Just use valSizer. + valCachedSizer := valSizer + if valIsPtr && !stdOptions && valType.Elem().Kind() == reflect.Struct { + u := getMarshalInfo(valType.Elem()) + valCachedSizer = func(ptr pointer, tagsize int) int { + // Same as message sizer, but use cache. + p := ptr.getPointer() + if p.isNil() { + return 0 + } + siz := u.cachedsize(p) + return siz + SizeVarint(uint64(siz)) + tagsize + } + } return func(ptr pointer, tagsize int) int { m := ptr.asPointerTo(t).Elem() // the map n := 0 @@ -2360,24 +2557,26 @@ func makeMapMarshaler(f *reflect.StructField) (sizer, marshaler) { if len(keys) > 1 && deterministic { sort.Sort(mapKeys(keys)) } + + var nerr nonFatal for _, k := range keys { ki := k.Interface() vi := m.MapIndex(k).Interface() kaddr := toAddrPointer(&ki, false) // pointer to key vaddr := toAddrPointer(&vi, valIsPtr) // pointer to value b = appendVarint(b, tag) - siz := keySizer(kaddr, 1) + valSizer(vaddr, 1) // tag of key = 1 (size=1), tag of val = 2 (size=1) + siz := keySizer(kaddr, 1) + valCachedSizer(vaddr, 1) // tag of key = 1 (size=1), tag of val = 2 (size=1) b = appendVarint(b, uint64(siz)) b, err = keyMarshaler(b, kaddr, keyWireTag, deterministic) - if err != nil { + if !nerr.Merge(err) { return b, err } b, err = valMarshaler(b, vaddr, valWireTag, deterministic) - if err != nil && err != ErrNil { // allow nil value in map + if err != ErrNil && !nerr.Merge(err) { // allow nil value in map return b, err } } - return b, nil + return b, nerr.E } } @@ -2450,6 +2649,7 @@ func (u *marshalInfo) appendExtensions(b []byte, ext *XXX_InternalExtensions, de defer mu.Unlock() var err error + var nerr nonFatal // Fast-path for common cases: zero or one extensions. // Don't bother sorting the keys. @@ -2469,11 +2669,11 @@ func (u *marshalInfo) appendExtensions(b []byte, ext *XXX_InternalExtensions, de v := e.value p := toAddrPointer(&v, ei.isptr) b, err = ei.marshaler(b, p, ei.wiretag, deterministic) - if err != nil { + if !nerr.Merge(err) { return b, err } } - return b, nil + return b, nerr.E } // Sort the keys to provide a deterministic encoding. @@ -2500,11 +2700,11 @@ func (u *marshalInfo) appendExtensions(b []byte, ext *XXX_InternalExtensions, de v := e.value p := toAddrPointer(&v, ei.isptr) b, err = ei.marshaler(b, p, ei.wiretag, deterministic) - if err != nil { + if !nerr.Merge(err) { return b, err } } - return b, nil + return b, nerr.E } // message set format is: @@ -2561,6 +2761,7 @@ func (u *marshalInfo) appendMessageSet(b []byte, ext *XXX_InternalExtensions, de defer mu.Unlock() var err error + var nerr nonFatal // Fast-path for common cases: zero or one extensions. // Don't bother sorting the keys. @@ -2587,12 +2788,12 @@ func (u *marshalInfo) appendMessageSet(b []byte, ext *XXX_InternalExtensions, de v := e.value p := toAddrPointer(&v, ei.isptr) b, err = ei.marshaler(b, p, 3<<3|WireBytes, deterministic) - if err != nil { + if !nerr.Merge(err) { return b, err } b = append(b, 1<<3|WireEndGroup) } - return b, nil + return b, nerr.E } // Sort the keys to provide a deterministic encoding. @@ -2626,11 +2827,11 @@ func (u *marshalInfo) appendMessageSet(b []byte, ext *XXX_InternalExtensions, de p := toAddrPointer(&v, ei.isptr) b, err = ei.marshaler(b, p, 3<<3|WireBytes, deterministic) b = append(b, 1<<3|WireEndGroup) - if err != nil { + if !nerr.Merge(err) { return b, err } } - return b, nil + return b, nerr.E } // sizeV1Extensions computes the size of encoded data for a V1-API extension field. @@ -2673,6 +2874,7 @@ func (u *marshalInfo) appendV1Extensions(b []byte, m map[int32]Extension, determ sort.Ints(keys) var err error + var nerr nonFatal for _, k := range keys { e := m[int32(k)] if e.value == nil || e.desc == nil { @@ -2689,11 +2891,11 @@ func (u *marshalInfo) appendV1Extensions(b []byte, m map[int32]Extension, determ v := e.value p := toAddrPointer(&v, ei.isptr) b, err = ei.marshaler(b, p, ei.wiretag, deterministic) - if err != nil { + if !nerr.Merge(err) { return b, err } } - return b, nil + return b, nerr.E } // newMarshaler is the interface representing objects that can marshal themselves. @@ -2758,6 +2960,11 @@ func Marshal(pb Message) ([]byte, error) { // a Buffer for most applications. func (p *Buffer) Marshal(pb Message) error { var err error + if p.deterministic { + if _, ok := pb.(Marshaler); ok { + return fmt.Errorf("proto: deterministic not supported by the Marshal method of %T", pb) + } + } if m, ok := pb.(newMarshaler); ok { siz := m.XXX_Size() p.grow(siz) // make sure buf has enough capacity diff --git a/vendor/github.com/gogo/protobuf/proto/table_unmarshal.go b/vendor/github.com/gogo/protobuf/proto/table_unmarshal.go index 910e2dd6ad3..e6b15c76cab 100644 --- a/vendor/github.com/gogo/protobuf/proto/table_unmarshal.go +++ b/vendor/github.com/gogo/protobuf/proto/table_unmarshal.go @@ -99,6 +99,8 @@ type unmarshalFieldInfo struct { // if a required field, contains a single set bit at this field's index in the required field list. reqMask uint64 + + name string // name of the field, for error reporting } var ( @@ -138,8 +140,8 @@ func (u *unmarshalInfo) unmarshal(m pointer, b []byte) error { if u.isMessageSet { return UnmarshalMessageSet(b, m.offset(u.extensions).toExtensions()) } - var reqMask uint64 // bitmask of required fields we've seen. - var rnse *RequiredNotSetError // an instance of a RequiredNotSetError returned by a submessage. + var reqMask uint64 // bitmask of required fields we've seen. + var errLater error for len(b) > 0 { // Read tag and wire type. // Special case 1 and 2 byte varints. @@ -178,11 +180,20 @@ func (u *unmarshalInfo) unmarshal(m pointer, b []byte) error { if r, ok := err.(*RequiredNotSetError); ok { // Remember this error, but keep parsing. We need to produce // a full parse even if a required field is missing. - rnse = r + if errLater == nil { + errLater = r + } reqMask |= f.reqMask continue } if err != errInternalBadWireType { + if err == errInvalidUTF8 { + if errLater == nil { + fullName := revProtoTypes[reflect.PtrTo(u.typ)] + "." + f.name + errLater = &invalidUTF8Error{fullName} + } + continue + } return err } // Fragments with bad wire type are treated as unknown fields. @@ -244,20 +255,16 @@ func (u *unmarshalInfo) unmarshal(m pointer, b []byte) error { emap[int32(tag)] = e } } - if rnse != nil { - // A required field of a submessage/group is missing. Return that error. - return rnse - } - if reqMask != u.reqMask { + if reqMask != u.reqMask && errLater == nil { // A required field of this message is missing. for _, n := range u.reqFields { if reqMask&1 == 0 { - return &RequiredNotSetError{n} + errLater = &RequiredNotSetError{n} } reqMask >>= 1 } } - return nil + return errLater } // computeUnmarshalInfo fills in u with information for use @@ -360,7 +367,7 @@ func (u *unmarshalInfo) computeUnmarshalInfo() { } // Store the info in the correct slot in the message. - u.setTag(tag, toField(&f), unmarshal, reqMask) + u.setTag(tag, toField(&f), unmarshal, reqMask, name) } // Find any types associated with oneof fields. @@ -376,10 +383,17 @@ func (u *unmarshalInfo) computeUnmarshalInfo() { f := typ.Field(0) // oneof implementers have one field baseUnmarshal := fieldUnmarshaler(&f) - tagstr := strings.Split(f.Tag.Get("protobuf"), ",")[1] - tag, err := strconv.Atoi(tagstr) + tags := strings.Split(f.Tag.Get("protobuf"), ",") + fieldNum, err := strconv.Atoi(tags[1]) if err != nil { - panic("protobuf tag field not an integer: " + tagstr) + panic("protobuf tag field not an integer: " + tags[1]) + } + var name string + for _, tag := range tags { + if strings.HasPrefix(tag, "name=") { + name = strings.TrimPrefix(tag, "name=") + break + } } // Find the oneof field that this struct implements. @@ -390,7 +404,7 @@ func (u *unmarshalInfo) computeUnmarshalInfo() { // That lets us know where this struct should be stored // when we encounter it during unmarshaling. unmarshal := makeUnmarshalOneof(typ, of.ityp, baseUnmarshal) - u.setTag(tag, of.field, unmarshal, 0) + u.setTag(fieldNum, of.field, unmarshal, 0, name) } } } @@ -411,7 +425,7 @@ func (u *unmarshalInfo) computeUnmarshalInfo() { // [0 0] is [tag=0/wiretype=varint varint-encoded-0]. u.setTag(0, zeroField, func(b []byte, f pointer, w int) ([]byte, error) { return nil, fmt.Errorf("proto: %s: illegal tag 0 (wire type %d)", t, w) - }, 0) + }, 0, "") // Set mask for required field check. u.reqMask = uint64(1)<= 0 && (tag < 16 || tag < 2*n) { // TODO: what are the right numbers here? for len(u.dense) <= tag { @@ -455,10 +470,16 @@ func typeUnmarshaler(t reflect.Type, tags string) unmarshaler { ctype := false isTime := false isDuration := false + isWktPointer := false + proto3 := false + validateUTF8 := true for _, tag := range tagArray[3:] { if strings.HasPrefix(tag, "name=") { name = tag[5:] } + if tag == "proto3" { + proto3 = true + } if strings.HasPrefix(tag, "customtype=") { ctype = true } @@ -468,7 +489,11 @@ func typeUnmarshaler(t reflect.Type, tags string) unmarshaler { if tag == "stdduration" { isDuration = true } + if tag == "wktptr" { + isWktPointer = true + } } + validateUTF8 = validateUTF8 && proto3 // Figure out packaging (pointer, slice, or both) slice := false @@ -522,6 +547,112 @@ func typeUnmarshaler(t reflect.Type, tags string) unmarshaler { return makeUnmarshalDuration(getUnmarshalInfo(t), name) } + if isWktPointer { + switch t.Kind() { + case reflect.Float64: + if pointer { + if slice { + return makeStdDoubleValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdDoubleValuePtrUnmarshaler(getUnmarshalInfo(t), name) + } + if slice { + return makeStdDoubleValueSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdDoubleValueUnmarshaler(getUnmarshalInfo(t), name) + case reflect.Float32: + if pointer { + if slice { + return makeStdFloatValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdFloatValuePtrUnmarshaler(getUnmarshalInfo(t), name) + } + if slice { + return makeStdFloatValueSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdFloatValueUnmarshaler(getUnmarshalInfo(t), name) + case reflect.Int64: + if pointer { + if slice { + return makeStdInt64ValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdInt64ValuePtrUnmarshaler(getUnmarshalInfo(t), name) + } + if slice { + return makeStdInt64ValueSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdInt64ValueUnmarshaler(getUnmarshalInfo(t), name) + case reflect.Uint64: + if pointer { + if slice { + return makeStdUInt64ValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdUInt64ValuePtrUnmarshaler(getUnmarshalInfo(t), name) + } + if slice { + return makeStdUInt64ValueSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdUInt64ValueUnmarshaler(getUnmarshalInfo(t), name) + case reflect.Int32: + if pointer { + if slice { + return makeStdInt32ValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdInt32ValuePtrUnmarshaler(getUnmarshalInfo(t), name) + } + if slice { + return makeStdInt32ValueSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdInt32ValueUnmarshaler(getUnmarshalInfo(t), name) + case reflect.Uint32: + if pointer { + if slice { + return makeStdUInt32ValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdUInt32ValuePtrUnmarshaler(getUnmarshalInfo(t), name) + } + if slice { + return makeStdUInt32ValueSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdUInt32ValueUnmarshaler(getUnmarshalInfo(t), name) + case reflect.Bool: + if pointer { + if slice { + return makeStdBoolValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdBoolValuePtrUnmarshaler(getUnmarshalInfo(t), name) + } + if slice { + return makeStdBoolValueSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdBoolValueUnmarshaler(getUnmarshalInfo(t), name) + case reflect.String: + if pointer { + if slice { + return makeStdStringValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdStringValuePtrUnmarshaler(getUnmarshalInfo(t), name) + } + if slice { + return makeStdStringValueSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdStringValueUnmarshaler(getUnmarshalInfo(t), name) + case uint8SliceType: + if pointer { + if slice { + return makeStdBytesValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdBytesValuePtrUnmarshaler(getUnmarshalInfo(t), name) + } + if slice { + return makeStdBytesValueSliceUnmarshaler(getUnmarshalInfo(t), name) + } + return makeStdBytesValueUnmarshaler(getUnmarshalInfo(t), name) + default: + panic(fmt.Sprintf("unknown wktpointer type %#v", t)) + } + } + // We'll never have both pointer and slice for basic types. if pointer && slice && t.Kind() != reflect.Struct { panic("both pointer and slice for basic type in " + t.Name()) @@ -656,6 +787,15 @@ func typeUnmarshaler(t reflect.Type, tags string) unmarshaler { } return unmarshalBytesValue case reflect.String: + if validateUTF8 { + if pointer { + return unmarshalUTF8StringPtr + } + if slice { + return unmarshalUTF8StringSlice + } + return unmarshalUTF8StringValue + } if pointer { return unmarshalStringPtr } @@ -1516,9 +1656,6 @@ func unmarshalStringValue(b []byte, f pointer, w int) ([]byte, error) { return nil, io.ErrUnexpectedEOF } v := string(b[:x]) - if !utf8.ValidString(v) { - return nil, errInvalidUTF8 - } *f.toString() = v return b[x:], nil } @@ -1536,9 +1673,6 @@ func unmarshalStringPtr(b []byte, f pointer, w int) ([]byte, error) { return nil, io.ErrUnexpectedEOF } v := string(b[:x]) - if !utf8.ValidString(v) { - return nil, errInvalidUTF8 - } *f.toStringPtr() = &v return b[x:], nil } @@ -1556,11 +1690,69 @@ func unmarshalStringSlice(b []byte, f pointer, w int) ([]byte, error) { return nil, io.ErrUnexpectedEOF } v := string(b[:x]) + s := f.toStringSlice() + *s = append(*s, v) + return b[x:], nil +} + +func unmarshalUTF8StringValue(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return b, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + v := string(b[:x]) + *f.toString() = v if !utf8.ValidString(v) { - return nil, errInvalidUTF8 + return b[x:], errInvalidUTF8 + } + return b[x:], nil +} + +func unmarshalUTF8StringPtr(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return b, errInternalBadWireType } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + v := string(b[:x]) + *f.toStringPtr() = &v + if !utf8.ValidString(v) { + return b[x:], errInvalidUTF8 + } + return b[x:], nil +} + +func unmarshalUTF8StringSlice(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return b, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + v := string(b[:x]) s := f.toStringSlice() *s = append(*s, v) + if !utf8.ValidString(v) { + return b[x:], errInvalidUTF8 + } return b[x:], nil } @@ -1731,6 +1923,9 @@ func makeUnmarshalMap(f *reflect.StructField) unmarshaler { if t == "stdduration" { valTags = append(valTags, t) } + if t == "wktptr" { + valTags = append(valTags, t) + } } unmarshalKey := typeUnmarshaler(kt, f.Tag.Get("protobuf_key")) unmarshalVal := typeUnmarshaler(vt, strings.Join(valTags, ",")) @@ -1755,6 +1950,7 @@ func makeUnmarshalMap(f *reflect.StructField) unmarshaler { // Maps will be somewhat slow. Oh well. // Read key and value from data. + var nerr nonFatal k := reflect.New(kt) v := reflect.New(vt) for len(b) > 0 { @@ -1775,7 +1971,7 @@ func makeUnmarshalMap(f *reflect.StructField) unmarshaler { err = errInternalBadWireType // skip unknown tag } - if err == nil { + if nerr.Merge(err) { continue } if err != errInternalBadWireType { @@ -1798,7 +1994,7 @@ func makeUnmarshalMap(f *reflect.StructField) unmarshaler { // Insert into map. m.SetMapIndex(k.Elem(), v.Elem()) - return r, nil + return r, nerr.E } } @@ -1824,15 +2020,16 @@ func makeUnmarshalOneof(typ, ityp reflect.Type, unmarshal unmarshaler) unmarshal // Unmarshal data into holder. // We unmarshal into the first field of the holder object. var err error + var nerr nonFatal b, err = unmarshal(b, valToPointer(v).offset(field0), w) - if err != nil { + if !nerr.Merge(err) { return nil, err } // Write pointer to holder into target field. f.asPointerTo(ityp).Elem().Set(v) - return b, nil + return b, nerr.E } } diff --git a/vendor/github.com/gogo/protobuf/proto/text.go b/vendor/github.com/gogo/protobuf/proto/text.go index 4f5706dc5f3..0407ba85d01 100644 --- a/vendor/github.com/gogo/protobuf/proto/text.go +++ b/vendor/github.com/gogo/protobuf/proto/text.go @@ -364,7 +364,7 @@ func (tm *TextMarshaler) writeStruct(w *textWriter, sv reflect.Value) error { return err } } - if err := tm.writeAny(w, key, props.mkeyprop); err != nil { + if err := tm.writeAny(w, key, props.MapKeyProp); err != nil { return err } if err := w.WriteByte('\n'); err != nil { @@ -381,7 +381,7 @@ func (tm *TextMarshaler) writeStruct(w *textWriter, sv reflect.Value) error { return err } } - if err := tm.writeAny(w, val, props.mvalprop); err != nil { + if err := tm.writeAny(w, val, props.MapValProp); err != nil { return err } if err := w.WriteByte('\n'); err != nil { diff --git a/vendor/github.com/gogo/protobuf/proto/text_parser.go b/vendor/github.com/gogo/protobuf/proto/text_parser.go index fbb000d3742..1ce0be2fa9b 100644 --- a/vendor/github.com/gogo/protobuf/proto/text_parser.go +++ b/vendor/github.com/gogo/protobuf/proto/text_parser.go @@ -636,17 +636,17 @@ func (p *textParser) readStruct(sv reflect.Value, terminator string) error { if err := p.consumeToken(":"); err != nil { return err } - if err := p.readAny(key, props.mkeyprop); err != nil { + if err := p.readAny(key, props.MapKeyProp); err != nil { return err } if err := p.consumeOptionalSeparator(); err != nil { return err } case "value": - if err := p.checkForColon(props.mvalprop, dst.Type().Elem()); err != nil { + if err := p.checkForColon(props.MapValProp, dst.Type().Elem()); err != nil { return err } - if err := p.readAny(val, props.mvalprop); err != nil { + if err := p.readAny(val, props.MapValProp); err != nil { return err } if err := p.consumeOptionalSeparator(); err != nil { @@ -923,6 +923,16 @@ func (p *textParser) readAny(v reflect.Value, props *Properties) error { fv.SetFloat(f) return nil } + case reflect.Int8: + if x, err := strconv.ParseInt(tok.value, 0, 8); err == nil { + fv.SetInt(x) + return nil + } + case reflect.Int16: + if x, err := strconv.ParseInt(tok.value, 0, 16); err == nil { + fv.SetInt(x) + return nil + } case reflect.Int32: if x, err := strconv.ParseInt(tok.value, 0, 32); err == nil { fv.SetInt(x) @@ -970,6 +980,16 @@ func (p *textParser) readAny(v reflect.Value, props *Properties) error { } // TODO: Handle nested messages which implement encoding.TextUnmarshaler. return p.readStruct(fv, terminator) + case reflect.Uint8: + if x, err := strconv.ParseUint(tok.value, 0, 8); err == nil { + fv.SetUint(x) + return nil + } + case reflect.Uint16: + if x, err := strconv.ParseUint(tok.value, 0, 16); err == nil { + fv.SetUint(x) + return nil + } case reflect.Uint32: if x, err := strconv.ParseUint(tok.value, 0, 32); err == nil { fv.SetUint(uint64(x)) diff --git a/vendor/github.com/gogo/protobuf/proto/wrappers.go b/vendor/github.com/gogo/protobuf/proto/wrappers.go new file mode 100644 index 00000000000..b175d1b6423 --- /dev/null +++ b/vendor/github.com/gogo/protobuf/proto/wrappers.go @@ -0,0 +1,1888 @@ +// Protocol Buffers for Go with Gadgets +// +// Copyright (c) 2018, The GoGo Authors. All rights reserved. +// http://github.com/gogo/protobuf +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package proto + +import ( + "io" + "reflect" +) + +func makeStdDoubleValueMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + t := ptr.asPointerTo(u.typ).Interface().(*float64) + v := &float64Value{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + t := ptr.asPointerTo(u.typ).Interface().(*float64) + v := &float64Value{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdDoubleValuePtrMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + if ptr.isNil() { + return 0 + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*float64) + v := &float64Value{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + if ptr.isNil() { + return b, nil + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*float64) + v := &float64Value{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdDoubleValueSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(u.typ) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(float64) + v := &float64Value{t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(u.typ) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(float64) + v := &float64Value{t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdDoubleValuePtrSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*float64) + v := &float64Value{*t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*float64) + v := &float64Value{*t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdDoubleValueUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &float64Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(sub.typ).Elem() + s.Set(reflect.ValueOf(m.Value)) + return b[x:], nil + } +} + +func makeStdDoubleValuePtrUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &float64Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(reflect.PtrTo(sub.typ)).Elem() + s.Set(reflect.ValueOf(&m.Value)) + return b[x:], nil + } +} + +func makeStdDoubleValuePtrSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &float64Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(reflect.PtrTo(sub.typ)) + newSlice := reflect.Append(slice, reflect.ValueOf(&m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} + +func makeStdDoubleValueSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &float64Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(sub.typ) + newSlice := reflect.Append(slice, reflect.ValueOf(m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} + +func makeStdFloatValueMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + t := ptr.asPointerTo(u.typ).Interface().(*float32) + v := &float32Value{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + t := ptr.asPointerTo(u.typ).Interface().(*float32) + v := &float32Value{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdFloatValuePtrMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + if ptr.isNil() { + return 0 + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*float32) + v := &float32Value{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + if ptr.isNil() { + return b, nil + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*float32) + v := &float32Value{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdFloatValueSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(u.typ) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(float32) + v := &float32Value{t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(u.typ) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(float32) + v := &float32Value{t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdFloatValuePtrSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*float32) + v := &float32Value{*t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*float32) + v := &float32Value{*t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdFloatValueUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &float32Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(sub.typ).Elem() + s.Set(reflect.ValueOf(m.Value)) + return b[x:], nil + } +} + +func makeStdFloatValuePtrUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &float32Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(reflect.PtrTo(sub.typ)).Elem() + s.Set(reflect.ValueOf(&m.Value)) + return b[x:], nil + } +} + +func makeStdFloatValuePtrSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &float32Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(reflect.PtrTo(sub.typ)) + newSlice := reflect.Append(slice, reflect.ValueOf(&m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} + +func makeStdFloatValueSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &float32Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(sub.typ) + newSlice := reflect.Append(slice, reflect.ValueOf(m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} + +func makeStdInt64ValueMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + t := ptr.asPointerTo(u.typ).Interface().(*int64) + v := &int64Value{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + t := ptr.asPointerTo(u.typ).Interface().(*int64) + v := &int64Value{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdInt64ValuePtrMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + if ptr.isNil() { + return 0 + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*int64) + v := &int64Value{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + if ptr.isNil() { + return b, nil + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*int64) + v := &int64Value{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdInt64ValueSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(u.typ) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(int64) + v := &int64Value{t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(u.typ) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(int64) + v := &int64Value{t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdInt64ValuePtrSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*int64) + v := &int64Value{*t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*int64) + v := &int64Value{*t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdInt64ValueUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &int64Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(sub.typ).Elem() + s.Set(reflect.ValueOf(m.Value)) + return b[x:], nil + } +} + +func makeStdInt64ValuePtrUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &int64Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(reflect.PtrTo(sub.typ)).Elem() + s.Set(reflect.ValueOf(&m.Value)) + return b[x:], nil + } +} + +func makeStdInt64ValuePtrSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &int64Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(reflect.PtrTo(sub.typ)) + newSlice := reflect.Append(slice, reflect.ValueOf(&m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} + +func makeStdInt64ValueSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &int64Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(sub.typ) + newSlice := reflect.Append(slice, reflect.ValueOf(m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} + +func makeStdUInt64ValueMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + t := ptr.asPointerTo(u.typ).Interface().(*uint64) + v := &uint64Value{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + t := ptr.asPointerTo(u.typ).Interface().(*uint64) + v := &uint64Value{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdUInt64ValuePtrMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + if ptr.isNil() { + return 0 + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*uint64) + v := &uint64Value{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + if ptr.isNil() { + return b, nil + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*uint64) + v := &uint64Value{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdUInt64ValueSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(u.typ) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(uint64) + v := &uint64Value{t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(u.typ) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(uint64) + v := &uint64Value{t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdUInt64ValuePtrSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*uint64) + v := &uint64Value{*t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*uint64) + v := &uint64Value{*t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdUInt64ValueUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &uint64Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(sub.typ).Elem() + s.Set(reflect.ValueOf(m.Value)) + return b[x:], nil + } +} + +func makeStdUInt64ValuePtrUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &uint64Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(reflect.PtrTo(sub.typ)).Elem() + s.Set(reflect.ValueOf(&m.Value)) + return b[x:], nil + } +} + +func makeStdUInt64ValuePtrSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &uint64Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(reflect.PtrTo(sub.typ)) + newSlice := reflect.Append(slice, reflect.ValueOf(&m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} + +func makeStdUInt64ValueSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &uint64Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(sub.typ) + newSlice := reflect.Append(slice, reflect.ValueOf(m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} + +func makeStdInt32ValueMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + t := ptr.asPointerTo(u.typ).Interface().(*int32) + v := &int32Value{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + t := ptr.asPointerTo(u.typ).Interface().(*int32) + v := &int32Value{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdInt32ValuePtrMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + if ptr.isNil() { + return 0 + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*int32) + v := &int32Value{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + if ptr.isNil() { + return b, nil + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*int32) + v := &int32Value{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdInt32ValueSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(u.typ) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(int32) + v := &int32Value{t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(u.typ) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(int32) + v := &int32Value{t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdInt32ValuePtrSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*int32) + v := &int32Value{*t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*int32) + v := &int32Value{*t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdInt32ValueUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &int32Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(sub.typ).Elem() + s.Set(reflect.ValueOf(m.Value)) + return b[x:], nil + } +} + +func makeStdInt32ValuePtrUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &int32Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(reflect.PtrTo(sub.typ)).Elem() + s.Set(reflect.ValueOf(&m.Value)) + return b[x:], nil + } +} + +func makeStdInt32ValuePtrSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &int32Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(reflect.PtrTo(sub.typ)) + newSlice := reflect.Append(slice, reflect.ValueOf(&m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} + +func makeStdInt32ValueSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &int32Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(sub.typ) + newSlice := reflect.Append(slice, reflect.ValueOf(m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} + +func makeStdUInt32ValueMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + t := ptr.asPointerTo(u.typ).Interface().(*uint32) + v := &uint32Value{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + t := ptr.asPointerTo(u.typ).Interface().(*uint32) + v := &uint32Value{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdUInt32ValuePtrMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + if ptr.isNil() { + return 0 + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*uint32) + v := &uint32Value{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + if ptr.isNil() { + return b, nil + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*uint32) + v := &uint32Value{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdUInt32ValueSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(u.typ) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(uint32) + v := &uint32Value{t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(u.typ) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(uint32) + v := &uint32Value{t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdUInt32ValuePtrSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*uint32) + v := &uint32Value{*t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*uint32) + v := &uint32Value{*t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdUInt32ValueUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &uint32Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(sub.typ).Elem() + s.Set(reflect.ValueOf(m.Value)) + return b[x:], nil + } +} + +func makeStdUInt32ValuePtrUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &uint32Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(reflect.PtrTo(sub.typ)).Elem() + s.Set(reflect.ValueOf(&m.Value)) + return b[x:], nil + } +} + +func makeStdUInt32ValuePtrSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &uint32Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(reflect.PtrTo(sub.typ)) + newSlice := reflect.Append(slice, reflect.ValueOf(&m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} + +func makeStdUInt32ValueSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &uint32Value{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(sub.typ) + newSlice := reflect.Append(slice, reflect.ValueOf(m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} + +func makeStdBoolValueMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + t := ptr.asPointerTo(u.typ).Interface().(*bool) + v := &boolValue{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + t := ptr.asPointerTo(u.typ).Interface().(*bool) + v := &boolValue{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdBoolValuePtrMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + if ptr.isNil() { + return 0 + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*bool) + v := &boolValue{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + if ptr.isNil() { + return b, nil + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*bool) + v := &boolValue{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdBoolValueSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(u.typ) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(bool) + v := &boolValue{t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(u.typ) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(bool) + v := &boolValue{t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdBoolValuePtrSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*bool) + v := &boolValue{*t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*bool) + v := &boolValue{*t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdBoolValueUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &boolValue{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(sub.typ).Elem() + s.Set(reflect.ValueOf(m.Value)) + return b[x:], nil + } +} + +func makeStdBoolValuePtrUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &boolValue{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(reflect.PtrTo(sub.typ)).Elem() + s.Set(reflect.ValueOf(&m.Value)) + return b[x:], nil + } +} + +func makeStdBoolValuePtrSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &boolValue{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(reflect.PtrTo(sub.typ)) + newSlice := reflect.Append(slice, reflect.ValueOf(&m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} + +func makeStdBoolValueSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &boolValue{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(sub.typ) + newSlice := reflect.Append(slice, reflect.ValueOf(m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} + +func makeStdStringValueMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + t := ptr.asPointerTo(u.typ).Interface().(*string) + v := &stringValue{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + t := ptr.asPointerTo(u.typ).Interface().(*string) + v := &stringValue{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdStringValuePtrMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + if ptr.isNil() { + return 0 + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*string) + v := &stringValue{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + if ptr.isNil() { + return b, nil + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*string) + v := &stringValue{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdStringValueSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(u.typ) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(string) + v := &stringValue{t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(u.typ) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(string) + v := &stringValue{t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdStringValuePtrSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*string) + v := &stringValue{*t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*string) + v := &stringValue{*t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdStringValueUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &stringValue{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(sub.typ).Elem() + s.Set(reflect.ValueOf(m.Value)) + return b[x:], nil + } +} + +func makeStdStringValuePtrUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &stringValue{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(reflect.PtrTo(sub.typ)).Elem() + s.Set(reflect.ValueOf(&m.Value)) + return b[x:], nil + } +} + +func makeStdStringValuePtrSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &stringValue{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(reflect.PtrTo(sub.typ)) + newSlice := reflect.Append(slice, reflect.ValueOf(&m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} + +func makeStdStringValueSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &stringValue{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(sub.typ) + newSlice := reflect.Append(slice, reflect.ValueOf(m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} + +func makeStdBytesValueMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + t := ptr.asPointerTo(u.typ).Interface().(*[]byte) + v := &bytesValue{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + t := ptr.asPointerTo(u.typ).Interface().(*[]byte) + v := &bytesValue{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdBytesValuePtrMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + if ptr.isNil() { + return 0 + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*[]byte) + v := &bytesValue{*t} + siz := Size(v) + return tagsize + SizeVarint(uint64(siz)) + siz + }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + if ptr.isNil() { + return b, nil + } + t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*[]byte) + v := &bytesValue{*t} + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(len(buf))) + b = append(b, buf...) + return b, nil + } +} + +func makeStdBytesValueSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(u.typ) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().([]byte) + v := &bytesValue{t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(u.typ) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().([]byte) + v := &bytesValue{t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdBytesValuePtrSliceMarshaler(u *marshalInfo) (sizer, marshaler) { + return func(ptr pointer, tagsize int) int { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + n := 0 + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*[]byte) + v := &bytesValue{*t} + siz := Size(v) + n += siz + SizeVarint(uint64(siz)) + tagsize + } + return n + }, + func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) { + s := ptr.getSlice(reflect.PtrTo(u.typ)) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i) + t := elem.Interface().(*[]byte) + v := &bytesValue{*t} + siz := Size(v) + buf, err := Marshal(v) + if err != nil { + return nil, err + } + b = appendVarint(b, wiretag) + b = appendVarint(b, uint64(siz)) + b = append(b, buf...) + } + + return b, nil + } +} + +func makeStdBytesValueUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &bytesValue{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(sub.typ).Elem() + s.Set(reflect.ValueOf(m.Value)) + return b[x:], nil + } +} + +func makeStdBytesValuePtrUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &bytesValue{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + s := f.asPointerTo(reflect.PtrTo(sub.typ)).Elem() + s.Set(reflect.ValueOf(&m.Value)) + return b[x:], nil + } +} + +func makeStdBytesValuePtrSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &bytesValue{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(reflect.PtrTo(sub.typ)) + newSlice := reflect.Append(slice, reflect.ValueOf(&m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} + +func makeStdBytesValueSliceUnmarshaler(sub *unmarshalInfo, name string) unmarshaler { + return func(b []byte, f pointer, w int) ([]byte, error) { + if w != WireBytes { + return nil, errInternalBadWireType + } + x, n := decodeVarint(b) + if n == 0 { + return nil, io.ErrUnexpectedEOF + } + b = b[n:] + if x > uint64(len(b)) { + return nil, io.ErrUnexpectedEOF + } + m := &bytesValue{} + if err := Unmarshal(b[:x], m); err != nil { + return nil, err + } + slice := f.getSlice(sub.typ) + newSlice := reflect.Append(slice, reflect.ValueOf(m.Value)) + slice.Set(newSlice) + return b[x:], nil + } +} diff --git a/vendor/github.com/gogo/protobuf/proto/wrappers_gogo.go b/vendor/github.com/gogo/protobuf/proto/wrappers_gogo.go new file mode 100644 index 00000000000..c1cf7bf85e9 --- /dev/null +++ b/vendor/github.com/gogo/protobuf/proto/wrappers_gogo.go @@ -0,0 +1,113 @@ +// Protocol Buffers for Go with Gadgets +// +// Copyright (c) 2018, The GoGo Authors. All rights reserved. +// http://github.com/gogo/protobuf +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package proto + +type float64Value struct { + Value float64 `protobuf:"fixed64,1,opt,name=value,proto3" json:"value,omitempty"` +} + +func (m *float64Value) Reset() { *m = float64Value{} } +func (*float64Value) ProtoMessage() {} +func (*float64Value) String() string { return "float64" } + +type float32Value struct { + Value float32 `protobuf:"fixed32,1,opt,name=value,proto3" json:"value,omitempty"` +} + +func (m *float32Value) Reset() { *m = float32Value{} } +func (*float32Value) ProtoMessage() {} +func (*float32Value) String() string { return "float32" } + +type int64Value struct { + Value int64 `protobuf:"varint,1,opt,name=value,proto3" json:"value,omitempty"` +} + +func (m *int64Value) Reset() { *m = int64Value{} } +func (*int64Value) ProtoMessage() {} +func (*int64Value) String() string { return "int64" } + +type uint64Value struct { + Value uint64 `protobuf:"varint,1,opt,name=value,proto3" json:"value,omitempty"` +} + +func (m *uint64Value) Reset() { *m = uint64Value{} } +func (*uint64Value) ProtoMessage() {} +func (*uint64Value) String() string { return "uint64" } + +type int32Value struct { + Value int32 `protobuf:"varint,1,opt,name=value,proto3" json:"value,omitempty"` +} + +func (m *int32Value) Reset() { *m = int32Value{} } +func (*int32Value) ProtoMessage() {} +func (*int32Value) String() string { return "int32" } + +type uint32Value struct { + Value uint32 `protobuf:"varint,1,opt,name=value,proto3" json:"value,omitempty"` +} + +func (m *uint32Value) Reset() { *m = uint32Value{} } +func (*uint32Value) ProtoMessage() {} +func (*uint32Value) String() string { return "uint32" } + +type boolValue struct { + Value bool `protobuf:"varint,1,opt,name=value,proto3" json:"value,omitempty"` +} + +func (m *boolValue) Reset() { *m = boolValue{} } +func (*boolValue) ProtoMessage() {} +func (*boolValue) String() string { return "bool" } + +type stringValue struct { + Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"` +} + +func (m *stringValue) Reset() { *m = stringValue{} } +func (*stringValue) ProtoMessage() {} +func (*stringValue) String() string { return "string" } + +type bytesValue struct { + Value []byte `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"` +} + +func (m *bytesValue) Reset() { *m = bytesValue{} } +func (*bytesValue) ProtoMessage() {} +func (*bytesValue) String() string { return "[]byte" } + +func init() { + RegisterType((*float64Value)(nil), "gogo.protobuf.proto.DoubleValue") + RegisterType((*float32Value)(nil), "gogo.protobuf.proto.FloatValue") + RegisterType((*int64Value)(nil), "gogo.protobuf.proto.Int64Value") + RegisterType((*uint64Value)(nil), "gogo.protobuf.proto.UInt64Value") + RegisterType((*int32Value)(nil), "gogo.protobuf.proto.Int32Value") + RegisterType((*uint32Value)(nil), "gogo.protobuf.proto.UInt32Value") + RegisterType((*boolValue)(nil), "gogo.protobuf.proto.BoolValue") + RegisterType((*stringValue)(nil), "gogo.protobuf.proto.StringValue") + RegisterType((*bytesValue)(nil), "gogo.protobuf.proto.BytesValue") +} diff --git a/vendor/github.com/golang/glog/LICENSE b/vendor/github.com/golang/glog/LICENSE deleted file mode 100644 index 37ec93a14fd..00000000000 --- a/vendor/github.com/golang/glog/LICENSE +++ /dev/null @@ -1,191 +0,0 @@ -Apache License -Version 2.0, January 2004 -http://www.apache.org/licenses/ - -TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - -1. Definitions. - -"License" shall mean the terms and conditions for use, reproduction, and -distribution as defined by Sections 1 through 9 of this document. - -"Licensor" shall mean the copyright owner or entity authorized by the copyright -owner that is granting the License. - -"Legal Entity" shall mean the union of the acting entity and all other entities -that control, are controlled by, or are under common control with that entity. -For the purposes of this definition, "control" means (i) the power, direct or -indirect, to cause the direction or management of such entity, whether by -contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the -outstanding shares, or (iii) beneficial ownership of such entity. - -"You" (or "Your") shall mean an individual or Legal Entity exercising -permissions granted by this License. - -"Source" form shall mean the preferred form for making modifications, including -but not limited to software source code, documentation source, and configuration -files. - -"Object" form shall mean any form resulting from mechanical transformation or -translation of a Source form, including but not limited to compiled object code, -generated documentation, and conversions to other media types. - -"Work" shall mean the work of authorship, whether in Source or Object form, made -available under the License, as indicated by a copyright notice that is included -in or attached to the work (an example is provided in the Appendix below). - -"Derivative Works" shall mean any work, whether in Source or Object form, that -is based on (or derived from) the Work and for which the editorial revisions, -annotations, elaborations, or other modifications represent, as a whole, an -original work of authorship. For the purposes of this License, Derivative Works -shall not include works that remain separable from, or merely link (or bind by -name) to the interfaces of, the Work and Derivative Works thereof. - -"Contribution" shall mean any work of authorship, including the original version -of the Work and any modifications or additions to that Work or Derivative Works -thereof, that is intentionally submitted to Licensor for inclusion in the Work -by the copyright owner or by an individual or Legal Entity authorized to submit -on behalf of the copyright owner. For the purposes of this definition, -"submitted" means any form of electronic, verbal, or written communication sent -to the Licensor or its representatives, including but not limited to -communication on electronic mailing lists, source code control systems, and -issue tracking systems that are managed by, or on behalf of, the Licensor for -the purpose of discussing and improving the Work, but excluding communication -that is conspicuously marked or otherwise designated in writing by the copyright -owner as "Not a Contribution." - -"Contributor" shall mean Licensor and any individual or Legal Entity on behalf -of whom a Contribution has been received by Licensor and subsequently -incorporated within the Work. - -2. Grant of Copyright License. - -Subject to the terms and conditions of this License, each Contributor hereby -grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, -irrevocable copyright license to reproduce, prepare Derivative Works of, -publicly display, publicly perform, sublicense, and distribute the Work and such -Derivative Works in Source or Object form. - -3. Grant of Patent License. - -Subject to the terms and conditions of this License, each Contributor hereby -grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, -irrevocable (except as stated in this section) patent license to make, have -made, use, offer to sell, sell, import, and otherwise transfer the Work, where -such license applies only to those patent claims licensable by such Contributor -that are necessarily infringed by their Contribution(s) alone or by combination -of their Contribution(s) with the Work to which such Contribution(s) was -submitted. If You institute patent litigation against any entity (including a -cross-claim or counterclaim in a lawsuit) alleging that the Work or a -Contribution incorporated within the Work constitutes direct or contributory -patent infringement, then any patent licenses granted to You under this License -for that Work shall terminate as of the date such litigation is filed. - -4. Redistribution. - -You may reproduce and distribute copies of the Work or Derivative Works thereof -in any medium, with or without modifications, and in Source or Object form, -provided that You meet the following conditions: - -You must give any other recipients of the Work or Derivative Works a copy of -this License; and -You must cause any modified files to carry prominent notices stating that You -changed the files; and -You must retain, in the Source form of any Derivative Works that You distribute, -all copyright, patent, trademark, and attribution notices from the Source form -of the Work, excluding those notices that do not pertain to any part of the -Derivative Works; and -If the Work includes a "NOTICE" text file as part of its distribution, then any -Derivative Works that You distribute must include a readable copy of the -attribution notices contained within such NOTICE file, excluding those notices -that do not pertain to any part of the Derivative Works, in at least one of the -following places: within a NOTICE text file distributed as part of the -Derivative Works; within the Source form or documentation, if provided along -with the Derivative Works; or, within a display generated by the Derivative -Works, if and wherever such third-party notices normally appear. The contents of -the NOTICE file are for informational purposes only and do not modify the -License. You may add Your own attribution notices within Derivative Works that -You distribute, alongside or as an addendum to the NOTICE text from the Work, -provided that such additional attribution notices cannot be construed as -modifying the License. -You may add Your own copyright statement to Your modifications and may provide -additional or different license terms and conditions for use, reproduction, or -distribution of Your modifications, or for any such Derivative Works as a whole, -provided Your use, reproduction, and distribution of the Work otherwise complies -with the conditions stated in this License. - -5. Submission of Contributions. - -Unless You explicitly state otherwise, any Contribution intentionally submitted -for inclusion in the Work by You to the Licensor shall be under the terms and -conditions of this License, without any additional terms or conditions. -Notwithstanding the above, nothing herein shall supersede or modify the terms of -any separate license agreement you may have executed with Licensor regarding -such Contributions. - -6. Trademarks. - -This License does not grant permission to use the trade names, trademarks, -service marks, or product names of the Licensor, except as required for -reasonable and customary use in describing the origin of the Work and -reproducing the content of the NOTICE file. - -7. Disclaimer of Warranty. - -Unless required by applicable law or agreed to in writing, Licensor provides the -Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, -including, without limitation, any warranties or conditions of TITLE, -NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are -solely responsible for determining the appropriateness of using or -redistributing the Work and assume any risks associated with Your exercise of -permissions under this License. - -8. Limitation of Liability. - -In no event and under no legal theory, whether in tort (including negligence), -contract, or otherwise, unless required by applicable law (such as deliberate -and grossly negligent acts) or agreed to in writing, shall any Contributor be -liable to You for damages, including any direct, indirect, special, incidental, -or consequential damages of any character arising as a result of this License or -out of the use or inability to use the Work (including but not limited to -damages for loss of goodwill, work stoppage, computer failure or malfunction, or -any and all other commercial damages or losses), even if such Contributor has -been advised of the possibility of such damages. - -9. Accepting Warranty or Additional Liability. - -While redistributing the Work or Derivative Works thereof, You may choose to -offer, and charge a fee for, acceptance of support, warranty, indemnity, or -other liability obligations and/or rights consistent with this License. However, -in accepting such obligations, You may act only on Your own behalf and on Your -sole responsibility, not on behalf of any other Contributor, and only if You -agree to indemnify, defend, and hold each Contributor harmless for any liability -incurred by, or claims asserted against, such Contributor by reason of your -accepting any such warranty or additional liability. - -END OF TERMS AND CONDITIONS - -APPENDIX: How to apply the Apache License to your work - -To apply the Apache License to your work, attach the following boilerplate -notice, with the fields enclosed by brackets "[]" replaced with your own -identifying information. (Don't include the brackets!) The text should be -enclosed in the appropriate comment syntax for the file format. We also -recommend that a file or class name and description of purpose be included on -the same "printed page" as the copyright notice for easier identification within -third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/vendor/github.com/golang/glog/README b/vendor/github.com/golang/glog/README deleted file mode 100644 index 387b4eb6890..00000000000 --- a/vendor/github.com/golang/glog/README +++ /dev/null @@ -1,44 +0,0 @@ -glog -==== - -Leveled execution logs for Go. - -This is an efficient pure Go implementation of leveled logs in the -manner of the open source C++ package - https://github.com/google/glog - -By binding methods to booleans it is possible to use the log package -without paying the expense of evaluating the arguments to the log. -Through the -vmodule flag, the package also provides fine-grained -control over logging at the file level. - -The comment from glog.go introduces the ideas: - - Package glog implements logging analogous to the Google-internal - C++ INFO/ERROR/V setup. It provides functions Info, Warning, - Error, Fatal, plus formatting variants such as Infof. It - also provides V-style logging controlled by the -v and - -vmodule=file=2 flags. - - Basic examples: - - glog.Info("Prepare to repel boarders") - - glog.Fatalf("Initialization failed: %s", err) - - See the documentation for the V function for an explanation - of these examples: - - if glog.V(2) { - glog.Info("Starting transaction...") - } - - glog.V(2).Infoln("Processed", nItems, "elements") - - -The repository contains an open source version of the log package -used inside Google. The master copy of the source lives inside -Google, not here. The code in this repo is for export only and is not itself -under development. Feature requests will be ignored. - -Send bug reports to golang-nuts@googlegroups.com. diff --git a/vendor/github.com/golang/glog/glog.go b/vendor/github.com/golang/glog/glog.go deleted file mode 100644 index 54bd7afdcab..00000000000 --- a/vendor/github.com/golang/glog/glog.go +++ /dev/null @@ -1,1180 +0,0 @@ -// Go support for leveled logs, analogous to https://code.google.com/p/google-glog/ -// -// Copyright 2013 Google Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Package glog implements logging analogous to the Google-internal C++ INFO/ERROR/V setup. -// It provides functions Info, Warning, Error, Fatal, plus formatting variants such as -// Infof. It also provides V-style logging controlled by the -v and -vmodule=file=2 flags. -// -// Basic examples: -// -// glog.Info("Prepare to repel boarders") -// -// glog.Fatalf("Initialization failed: %s", err) -// -// See the documentation for the V function for an explanation of these examples: -// -// if glog.V(2) { -// glog.Info("Starting transaction...") -// } -// -// glog.V(2).Infoln("Processed", nItems, "elements") -// -// Log output is buffered and written periodically using Flush. Programs -// should call Flush before exiting to guarantee all log output is written. -// -// By default, all log statements write to files in a temporary directory. -// This package provides several flags that modify this behavior. -// As a result, flag.Parse must be called before any logging is done. -// -// -logtostderr=false -// Logs are written to standard error instead of to files. -// -alsologtostderr=false -// Logs are written to standard error as well as to files. -// -stderrthreshold=ERROR -// Log events at or above this severity are logged to standard -// error as well as to files. -// -log_dir="" -// Log files will be written to this directory instead of the -// default temporary directory. -// -// Other flags provide aids to debugging. -// -// -log_backtrace_at="" -// When set to a file and line number holding a logging statement, -// such as -// -log_backtrace_at=gopherflakes.go:234 -// a stack trace will be written to the Info log whenever execution -// hits that statement. (Unlike with -vmodule, the ".go" must be -// present.) -// -v=0 -// Enable V-leveled logging at the specified level. -// -vmodule="" -// The syntax of the argument is a comma-separated list of pattern=N, -// where pattern is a literal file name (minus the ".go" suffix) or -// "glob" pattern and N is a V level. For instance, -// -vmodule=gopher*=3 -// sets the V level to 3 in all Go files whose names begin "gopher". -// -package glog - -import ( - "bufio" - "bytes" - "errors" - "flag" - "fmt" - "io" - stdLog "log" - "os" - "path/filepath" - "runtime" - "strconv" - "strings" - "sync" - "sync/atomic" - "time" -) - -// severity identifies the sort of log: info, warning etc. It also implements -// the flag.Value interface. The -stderrthreshold flag is of type severity and -// should be modified only through the flag.Value interface. The values match -// the corresponding constants in C++. -type severity int32 // sync/atomic int32 - -// These constants identify the log levels in order of increasing severity. -// A message written to a high-severity log file is also written to each -// lower-severity log file. -const ( - infoLog severity = iota - warningLog - errorLog - fatalLog - numSeverity = 4 -) - -const severityChar = "IWEF" - -var severityName = []string{ - infoLog: "INFO", - warningLog: "WARNING", - errorLog: "ERROR", - fatalLog: "FATAL", -} - -// get returns the value of the severity. -func (s *severity) get() severity { - return severity(atomic.LoadInt32((*int32)(s))) -} - -// set sets the value of the severity. -func (s *severity) set(val severity) { - atomic.StoreInt32((*int32)(s), int32(val)) -} - -// String is part of the flag.Value interface. -func (s *severity) String() string { - return strconv.FormatInt(int64(*s), 10) -} - -// Get is part of the flag.Value interface. -func (s *severity) Get() interface{} { - return *s -} - -// Set is part of the flag.Value interface. -func (s *severity) Set(value string) error { - var threshold severity - // Is it a known name? - if v, ok := severityByName(value); ok { - threshold = v - } else { - v, err := strconv.Atoi(value) - if err != nil { - return err - } - threshold = severity(v) - } - logging.stderrThreshold.set(threshold) - return nil -} - -func severityByName(s string) (severity, bool) { - s = strings.ToUpper(s) - for i, name := range severityName { - if name == s { - return severity(i), true - } - } - return 0, false -} - -// OutputStats tracks the number of output lines and bytes written. -type OutputStats struct { - lines int64 - bytes int64 -} - -// Lines returns the number of lines written. -func (s *OutputStats) Lines() int64 { - return atomic.LoadInt64(&s.lines) -} - -// Bytes returns the number of bytes written. -func (s *OutputStats) Bytes() int64 { - return atomic.LoadInt64(&s.bytes) -} - -// Stats tracks the number of lines of output and number of bytes -// per severity level. Values must be read with atomic.LoadInt64. -var Stats struct { - Info, Warning, Error OutputStats -} - -var severityStats = [numSeverity]*OutputStats{ - infoLog: &Stats.Info, - warningLog: &Stats.Warning, - errorLog: &Stats.Error, -} - -// Level is exported because it appears in the arguments to V and is -// the type of the v flag, which can be set programmatically. -// It's a distinct type because we want to discriminate it from logType. -// Variables of type level are only changed under logging.mu. -// The -v flag is read only with atomic ops, so the state of the logging -// module is consistent. - -// Level is treated as a sync/atomic int32. - -// Level specifies a level of verbosity for V logs. *Level implements -// flag.Value; the -v flag is of type Level and should be modified -// only through the flag.Value interface. -type Level int32 - -// get returns the value of the Level. -func (l *Level) get() Level { - return Level(atomic.LoadInt32((*int32)(l))) -} - -// set sets the value of the Level. -func (l *Level) set(val Level) { - atomic.StoreInt32((*int32)(l), int32(val)) -} - -// String is part of the flag.Value interface. -func (l *Level) String() string { - return strconv.FormatInt(int64(*l), 10) -} - -// Get is part of the flag.Value interface. -func (l *Level) Get() interface{} { - return *l -} - -// Set is part of the flag.Value interface. -func (l *Level) Set(value string) error { - v, err := strconv.Atoi(value) - if err != nil { - return err - } - logging.mu.Lock() - defer logging.mu.Unlock() - logging.setVState(Level(v), logging.vmodule.filter, false) - return nil -} - -// moduleSpec represents the setting of the -vmodule flag. -type moduleSpec struct { - filter []modulePat -} - -// modulePat contains a filter for the -vmodule flag. -// It holds a verbosity level and a file pattern to match. -type modulePat struct { - pattern string - literal bool // The pattern is a literal string - level Level -} - -// match reports whether the file matches the pattern. It uses a string -// comparison if the pattern contains no metacharacters. -func (m *modulePat) match(file string) bool { - if m.literal { - return file == m.pattern - } - match, _ := filepath.Match(m.pattern, file) - return match -} - -func (m *moduleSpec) String() string { - // Lock because the type is not atomic. TODO: clean this up. - logging.mu.Lock() - defer logging.mu.Unlock() - var b bytes.Buffer - for i, f := range m.filter { - if i > 0 { - b.WriteRune(',') - } - fmt.Fprintf(&b, "%s=%d", f.pattern, f.level) - } - return b.String() -} - -// Get is part of the (Go 1.2) flag.Getter interface. It always returns nil for this flag type since the -// struct is not exported. -func (m *moduleSpec) Get() interface{} { - return nil -} - -var errVmoduleSyntax = errors.New("syntax error: expect comma-separated list of filename=N") - -// Syntax: -vmodule=recordio=2,file=1,gfs*=3 -func (m *moduleSpec) Set(value string) error { - var filter []modulePat - for _, pat := range strings.Split(value, ",") { - if len(pat) == 0 { - // Empty strings such as from a trailing comma can be ignored. - continue - } - patLev := strings.Split(pat, "=") - if len(patLev) != 2 || len(patLev[0]) == 0 || len(patLev[1]) == 0 { - return errVmoduleSyntax - } - pattern := patLev[0] - v, err := strconv.Atoi(patLev[1]) - if err != nil { - return errors.New("syntax error: expect comma-separated list of filename=N") - } - if v < 0 { - return errors.New("negative value for vmodule level") - } - if v == 0 { - continue // Ignore. It's harmless but no point in paying the overhead. - } - // TODO: check syntax of filter? - filter = append(filter, modulePat{pattern, isLiteral(pattern), Level(v)}) - } - logging.mu.Lock() - defer logging.mu.Unlock() - logging.setVState(logging.verbosity, filter, true) - return nil -} - -// isLiteral reports whether the pattern is a literal string, that is, has no metacharacters -// that require filepath.Match to be called to match the pattern. -func isLiteral(pattern string) bool { - return !strings.ContainsAny(pattern, `\*?[]`) -} - -// traceLocation represents the setting of the -log_backtrace_at flag. -type traceLocation struct { - file string - line int -} - -// isSet reports whether the trace location has been specified. -// logging.mu is held. -func (t *traceLocation) isSet() bool { - return t.line > 0 -} - -// match reports whether the specified file and line matches the trace location. -// The argument file name is the full path, not the basename specified in the flag. -// logging.mu is held. -func (t *traceLocation) match(file string, line int) bool { - if t.line != line { - return false - } - if i := strings.LastIndex(file, "/"); i >= 0 { - file = file[i+1:] - } - return t.file == file -} - -func (t *traceLocation) String() string { - // Lock because the type is not atomic. TODO: clean this up. - logging.mu.Lock() - defer logging.mu.Unlock() - return fmt.Sprintf("%s:%d", t.file, t.line) -} - -// Get is part of the (Go 1.2) flag.Getter interface. It always returns nil for this flag type since the -// struct is not exported -func (t *traceLocation) Get() interface{} { - return nil -} - -var errTraceSyntax = errors.New("syntax error: expect file.go:234") - -// Syntax: -log_backtrace_at=gopherflakes.go:234 -// Note that unlike vmodule the file extension is included here. -func (t *traceLocation) Set(value string) error { - if value == "" { - // Unset. - t.line = 0 - t.file = "" - } - fields := strings.Split(value, ":") - if len(fields) != 2 { - return errTraceSyntax - } - file, line := fields[0], fields[1] - if !strings.Contains(file, ".") { - return errTraceSyntax - } - v, err := strconv.Atoi(line) - if err != nil { - return errTraceSyntax - } - if v <= 0 { - return errors.New("negative or zero value for level") - } - logging.mu.Lock() - defer logging.mu.Unlock() - t.line = v - t.file = file - return nil -} - -// flushSyncWriter is the interface satisfied by logging destinations. -type flushSyncWriter interface { - Flush() error - Sync() error - io.Writer -} - -func init() { - flag.BoolVar(&logging.toStderr, "logtostderr", false, "log to standard error instead of files") - flag.BoolVar(&logging.alsoToStderr, "alsologtostderr", false, "log to standard error as well as files") - flag.Var(&logging.verbosity, "v", "log level for V logs") - flag.Var(&logging.stderrThreshold, "stderrthreshold", "logs at or above this threshold go to stderr") - flag.Var(&logging.vmodule, "vmodule", "comma-separated list of pattern=N settings for file-filtered logging") - flag.Var(&logging.traceLocation, "log_backtrace_at", "when logging hits line file:N, emit a stack trace") - - // Default stderrThreshold is ERROR. - logging.stderrThreshold = errorLog - - logging.setVState(0, nil, false) - go logging.flushDaemon() -} - -// Flush flushes all pending log I/O. -func Flush() { - logging.lockAndFlushAll() -} - -// loggingT collects all the global state of the logging setup. -type loggingT struct { - // Boolean flags. Not handled atomically because the flag.Value interface - // does not let us avoid the =true, and that shorthand is necessary for - // compatibility. TODO: does this matter enough to fix? Seems unlikely. - toStderr bool // The -logtostderr flag. - alsoToStderr bool // The -alsologtostderr flag. - - // Level flag. Handled atomically. - stderrThreshold severity // The -stderrthreshold flag. - - // freeList is a list of byte buffers, maintained under freeListMu. - freeList *buffer - // freeListMu maintains the free list. It is separate from the main mutex - // so buffers can be grabbed and printed to without holding the main lock, - // for better parallelization. - freeListMu sync.Mutex - - // mu protects the remaining elements of this structure and is - // used to synchronize logging. - mu sync.Mutex - // file holds writer for each of the log types. - file [numSeverity]flushSyncWriter - // pcs is used in V to avoid an allocation when computing the caller's PC. - pcs [1]uintptr - // vmap is a cache of the V Level for each V() call site, identified by PC. - // It is wiped whenever the vmodule flag changes state. - vmap map[uintptr]Level - // filterLength stores the length of the vmodule filter chain. If greater - // than zero, it means vmodule is enabled. It may be read safely - // using sync.LoadInt32, but is only modified under mu. - filterLength int32 - // traceLocation is the state of the -log_backtrace_at flag. - traceLocation traceLocation - // These flags are modified only under lock, although verbosity may be fetched - // safely using atomic.LoadInt32. - vmodule moduleSpec // The state of the -vmodule flag. - verbosity Level // V logging level, the value of the -v flag/ -} - -// buffer holds a byte Buffer for reuse. The zero value is ready for use. -type buffer struct { - bytes.Buffer - tmp [64]byte // temporary byte array for creating headers. - next *buffer -} - -var logging loggingT - -// setVState sets a consistent state for V logging. -// l.mu is held. -func (l *loggingT) setVState(verbosity Level, filter []modulePat, setFilter bool) { - // Turn verbosity off so V will not fire while we are in transition. - logging.verbosity.set(0) - // Ditto for filter length. - atomic.StoreInt32(&logging.filterLength, 0) - - // Set the new filters and wipe the pc->Level map if the filter has changed. - if setFilter { - logging.vmodule.filter = filter - logging.vmap = make(map[uintptr]Level) - } - - // Things are consistent now, so enable filtering and verbosity. - // They are enabled in order opposite to that in V. - atomic.StoreInt32(&logging.filterLength, int32(len(filter))) - logging.verbosity.set(verbosity) -} - -// getBuffer returns a new, ready-to-use buffer. -func (l *loggingT) getBuffer() *buffer { - l.freeListMu.Lock() - b := l.freeList - if b != nil { - l.freeList = b.next - } - l.freeListMu.Unlock() - if b == nil { - b = new(buffer) - } else { - b.next = nil - b.Reset() - } - return b -} - -// putBuffer returns a buffer to the free list. -func (l *loggingT) putBuffer(b *buffer) { - if b.Len() >= 256 { - // Let big buffers die a natural death. - return - } - l.freeListMu.Lock() - b.next = l.freeList - l.freeList = b - l.freeListMu.Unlock() -} - -var timeNow = time.Now // Stubbed out for testing. - -/* -header formats a log header as defined by the C++ implementation. -It returns a buffer containing the formatted header and the user's file and line number. -The depth specifies how many stack frames above lives the source line to be identified in the log message. - -Log lines have this form: - Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg... -where the fields are defined as follows: - L A single character, representing the log level (eg 'I' for INFO) - mm The month (zero padded; ie May is '05') - dd The day (zero padded) - hh:mm:ss.uuuuuu Time in hours, minutes and fractional seconds - threadid The space-padded thread ID as returned by GetTID() - file The file name - line The line number - msg The user-supplied message -*/ -func (l *loggingT) header(s severity, depth int) (*buffer, string, int) { - _, file, line, ok := runtime.Caller(3 + depth) - if !ok { - file = "???" - line = 1 - } else { - slash := strings.LastIndex(file, "/") - if slash >= 0 { - file = file[slash+1:] - } - } - return l.formatHeader(s, file, line), file, line -} - -// formatHeader formats a log header using the provided file name and line number. -func (l *loggingT) formatHeader(s severity, file string, line int) *buffer { - now := timeNow() - if line < 0 { - line = 0 // not a real line number, but acceptable to someDigits - } - if s > fatalLog { - s = infoLog // for safety. - } - buf := l.getBuffer() - - // Avoid Fprintf, for speed. The format is so simple that we can do it quickly by hand. - // It's worth about 3X. Fprintf is hard. - _, month, day := now.Date() - hour, minute, second := now.Clock() - // Lmmdd hh:mm:ss.uuuuuu threadid file:line] - buf.tmp[0] = severityChar[s] - buf.twoDigits(1, int(month)) - buf.twoDigits(3, day) - buf.tmp[5] = ' ' - buf.twoDigits(6, hour) - buf.tmp[8] = ':' - buf.twoDigits(9, minute) - buf.tmp[11] = ':' - buf.twoDigits(12, second) - buf.tmp[14] = '.' - buf.nDigits(6, 15, now.Nanosecond()/1000, '0') - buf.tmp[21] = ' ' - buf.nDigits(7, 22, pid, ' ') // TODO: should be TID - buf.tmp[29] = ' ' - buf.Write(buf.tmp[:30]) - buf.WriteString(file) - buf.tmp[0] = ':' - n := buf.someDigits(1, line) - buf.tmp[n+1] = ']' - buf.tmp[n+2] = ' ' - buf.Write(buf.tmp[:n+3]) - return buf -} - -// Some custom tiny helper functions to print the log header efficiently. - -const digits = "0123456789" - -// twoDigits formats a zero-prefixed two-digit integer at buf.tmp[i]. -func (buf *buffer) twoDigits(i, d int) { - buf.tmp[i+1] = digits[d%10] - d /= 10 - buf.tmp[i] = digits[d%10] -} - -// nDigits formats an n-digit integer at buf.tmp[i], -// padding with pad on the left. -// It assumes d >= 0. -func (buf *buffer) nDigits(n, i, d int, pad byte) { - j := n - 1 - for ; j >= 0 && d > 0; j-- { - buf.tmp[i+j] = digits[d%10] - d /= 10 - } - for ; j >= 0; j-- { - buf.tmp[i+j] = pad - } -} - -// someDigits formats a zero-prefixed variable-width integer at buf.tmp[i]. -func (buf *buffer) someDigits(i, d int) int { - // Print into the top, then copy down. We know there's space for at least - // a 10-digit number. - j := len(buf.tmp) - for { - j-- - buf.tmp[j] = digits[d%10] - d /= 10 - if d == 0 { - break - } - } - return copy(buf.tmp[i:], buf.tmp[j:]) -} - -func (l *loggingT) println(s severity, args ...interface{}) { - buf, file, line := l.header(s, 0) - fmt.Fprintln(buf, args...) - l.output(s, buf, file, line, false) -} - -func (l *loggingT) print(s severity, args ...interface{}) { - l.printDepth(s, 1, args...) -} - -func (l *loggingT) printDepth(s severity, depth int, args ...interface{}) { - buf, file, line := l.header(s, depth) - fmt.Fprint(buf, args...) - if buf.Bytes()[buf.Len()-1] != '\n' { - buf.WriteByte('\n') - } - l.output(s, buf, file, line, false) -} - -func (l *loggingT) printf(s severity, format string, args ...interface{}) { - buf, file, line := l.header(s, 0) - fmt.Fprintf(buf, format, args...) - if buf.Bytes()[buf.Len()-1] != '\n' { - buf.WriteByte('\n') - } - l.output(s, buf, file, line, false) -} - -// printWithFileLine behaves like print but uses the provided file and line number. If -// alsoLogToStderr is true, the log message always appears on standard error; it -// will also appear in the log file unless --logtostderr is set. -func (l *loggingT) printWithFileLine(s severity, file string, line int, alsoToStderr bool, args ...interface{}) { - buf := l.formatHeader(s, file, line) - fmt.Fprint(buf, args...) - if buf.Bytes()[buf.Len()-1] != '\n' { - buf.WriteByte('\n') - } - l.output(s, buf, file, line, alsoToStderr) -} - -// output writes the data to the log files and releases the buffer. -func (l *loggingT) output(s severity, buf *buffer, file string, line int, alsoToStderr bool) { - l.mu.Lock() - if l.traceLocation.isSet() { - if l.traceLocation.match(file, line) { - buf.Write(stacks(false)) - } - } - data := buf.Bytes() - if !flag.Parsed() { - os.Stderr.Write([]byte("ERROR: logging before flag.Parse: ")) - os.Stderr.Write(data) - } else if l.toStderr { - os.Stderr.Write(data) - } else { - if alsoToStderr || l.alsoToStderr || s >= l.stderrThreshold.get() { - os.Stderr.Write(data) - } - if l.file[s] == nil { - if err := l.createFiles(s); err != nil { - os.Stderr.Write(data) // Make sure the message appears somewhere. - l.exit(err) - } - } - switch s { - case fatalLog: - l.file[fatalLog].Write(data) - fallthrough - case errorLog: - l.file[errorLog].Write(data) - fallthrough - case warningLog: - l.file[warningLog].Write(data) - fallthrough - case infoLog: - l.file[infoLog].Write(data) - } - } - if s == fatalLog { - // If we got here via Exit rather than Fatal, print no stacks. - if atomic.LoadUint32(&fatalNoStacks) > 0 { - l.mu.Unlock() - timeoutFlush(10 * time.Second) - os.Exit(1) - } - // Dump all goroutine stacks before exiting. - // First, make sure we see the trace for the current goroutine on standard error. - // If -logtostderr has been specified, the loop below will do that anyway - // as the first stack in the full dump. - if !l.toStderr { - os.Stderr.Write(stacks(false)) - } - // Write the stack trace for all goroutines to the files. - trace := stacks(true) - logExitFunc = func(error) {} // If we get a write error, we'll still exit below. - for log := fatalLog; log >= infoLog; log-- { - if f := l.file[log]; f != nil { // Can be nil if -logtostderr is set. - f.Write(trace) - } - } - l.mu.Unlock() - timeoutFlush(10 * time.Second) - os.Exit(255) // C++ uses -1, which is silly because it's anded with 255 anyway. - } - l.putBuffer(buf) - l.mu.Unlock() - if stats := severityStats[s]; stats != nil { - atomic.AddInt64(&stats.lines, 1) - atomic.AddInt64(&stats.bytes, int64(len(data))) - } -} - -// timeoutFlush calls Flush and returns when it completes or after timeout -// elapses, whichever happens first. This is needed because the hooks invoked -// by Flush may deadlock when glog.Fatal is called from a hook that holds -// a lock. -func timeoutFlush(timeout time.Duration) { - done := make(chan bool, 1) - go func() { - Flush() // calls logging.lockAndFlushAll() - done <- true - }() - select { - case <-done: - case <-time.After(timeout): - fmt.Fprintln(os.Stderr, "glog: Flush took longer than", timeout) - } -} - -// stacks is a wrapper for runtime.Stack that attempts to recover the data for all goroutines. -func stacks(all bool) []byte { - // We don't know how big the traces are, so grow a few times if they don't fit. Start large, though. - n := 10000 - if all { - n = 100000 - } - var trace []byte - for i := 0; i < 5; i++ { - trace = make([]byte, n) - nbytes := runtime.Stack(trace, all) - if nbytes < len(trace) { - return trace[:nbytes] - } - n *= 2 - } - return trace -} - -// logExitFunc provides a simple mechanism to override the default behavior -// of exiting on error. Used in testing and to guarantee we reach a required exit -// for fatal logs. Instead, exit could be a function rather than a method but that -// would make its use clumsier. -var logExitFunc func(error) - -// exit is called if there is trouble creating or writing log files. -// It flushes the logs and exits the program; there's no point in hanging around. -// l.mu is held. -func (l *loggingT) exit(err error) { - fmt.Fprintf(os.Stderr, "log: exiting because of error: %s\n", err) - // If logExitFunc is set, we do that instead of exiting. - if logExitFunc != nil { - logExitFunc(err) - return - } - l.flushAll() - os.Exit(2) -} - -// syncBuffer joins a bufio.Writer to its underlying file, providing access to the -// file's Sync method and providing a wrapper for the Write method that provides log -// file rotation. There are conflicting methods, so the file cannot be embedded. -// l.mu is held for all its methods. -type syncBuffer struct { - logger *loggingT - *bufio.Writer - file *os.File - sev severity - nbytes uint64 // The number of bytes written to this file -} - -func (sb *syncBuffer) Sync() error { - return sb.file.Sync() -} - -func (sb *syncBuffer) Write(p []byte) (n int, err error) { - if sb.nbytes+uint64(len(p)) >= MaxSize { - if err := sb.rotateFile(time.Now()); err != nil { - sb.logger.exit(err) - } - } - n, err = sb.Writer.Write(p) - sb.nbytes += uint64(n) - if err != nil { - sb.logger.exit(err) - } - return -} - -// rotateFile closes the syncBuffer's file and starts a new one. -func (sb *syncBuffer) rotateFile(now time.Time) error { - if sb.file != nil { - sb.Flush() - sb.file.Close() - } - var err error - sb.file, _, err = create(severityName[sb.sev], now) - sb.nbytes = 0 - if err != nil { - return err - } - - sb.Writer = bufio.NewWriterSize(sb.file, bufferSize) - - // Write header. - var buf bytes.Buffer - fmt.Fprintf(&buf, "Log file created at: %s\n", now.Format("2006/01/02 15:04:05")) - fmt.Fprintf(&buf, "Running on machine: %s\n", host) - fmt.Fprintf(&buf, "Binary: Built with %s %s for %s/%s\n", runtime.Compiler, runtime.Version(), runtime.GOOS, runtime.GOARCH) - fmt.Fprintf(&buf, "Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg\n") - n, err := sb.file.Write(buf.Bytes()) - sb.nbytes += uint64(n) - return err -} - -// bufferSize sizes the buffer associated with each log file. It's large -// so that log records can accumulate without the logging thread blocking -// on disk I/O. The flushDaemon will block instead. -const bufferSize = 256 * 1024 - -// createFiles creates all the log files for severity from sev down to infoLog. -// l.mu is held. -func (l *loggingT) createFiles(sev severity) error { - now := time.Now() - // Files are created in decreasing severity order, so as soon as we find one - // has already been created, we can stop. - for s := sev; s >= infoLog && l.file[s] == nil; s-- { - sb := &syncBuffer{ - logger: l, - sev: s, - } - if err := sb.rotateFile(now); err != nil { - return err - } - l.file[s] = sb - } - return nil -} - -const flushInterval = 30 * time.Second - -// flushDaemon periodically flushes the log file buffers. -func (l *loggingT) flushDaemon() { - for _ = range time.NewTicker(flushInterval).C { - l.lockAndFlushAll() - } -} - -// lockAndFlushAll is like flushAll but locks l.mu first. -func (l *loggingT) lockAndFlushAll() { - l.mu.Lock() - l.flushAll() - l.mu.Unlock() -} - -// flushAll flushes all the logs and attempts to "sync" their data to disk. -// l.mu is held. -func (l *loggingT) flushAll() { - // Flush from fatal down, in case there's trouble flushing. - for s := fatalLog; s >= infoLog; s-- { - file := l.file[s] - if file != nil { - file.Flush() // ignore error - file.Sync() // ignore error - } - } -} - -// CopyStandardLogTo arranges for messages written to the Go "log" package's -// default logs to also appear in the Google logs for the named and lower -// severities. Subsequent changes to the standard log's default output location -// or format may break this behavior. -// -// Valid names are "INFO", "WARNING", "ERROR", and "FATAL". If the name is not -// recognized, CopyStandardLogTo panics. -func CopyStandardLogTo(name string) { - sev, ok := severityByName(name) - if !ok { - panic(fmt.Sprintf("log.CopyStandardLogTo(%q): unrecognized severity name", name)) - } - // Set a log format that captures the user's file and line: - // d.go:23: message - stdLog.SetFlags(stdLog.Lshortfile) - stdLog.SetOutput(logBridge(sev)) -} - -// logBridge provides the Write method that enables CopyStandardLogTo to connect -// Go's standard logs to the logs provided by this package. -type logBridge severity - -// Write parses the standard logging line and passes its components to the -// logger for severity(lb). -func (lb logBridge) Write(b []byte) (n int, err error) { - var ( - file = "???" - line = 1 - text string - ) - // Split "d.go:23: message" into "d.go", "23", and "message". - if parts := bytes.SplitN(b, []byte{':'}, 3); len(parts) != 3 || len(parts[0]) < 1 || len(parts[2]) < 1 { - text = fmt.Sprintf("bad log format: %s", b) - } else { - file = string(parts[0]) - text = string(parts[2][1:]) // skip leading space - line, err = strconv.Atoi(string(parts[1])) - if err != nil { - text = fmt.Sprintf("bad line number: %s", b) - line = 1 - } - } - // printWithFileLine with alsoToStderr=true, so standard log messages - // always appear on standard error. - logging.printWithFileLine(severity(lb), file, line, true, text) - return len(b), nil -} - -// setV computes and remembers the V level for a given PC -// when vmodule is enabled. -// File pattern matching takes the basename of the file, stripped -// of its .go suffix, and uses filepath.Match, which is a little more -// general than the *? matching used in C++. -// l.mu is held. -func (l *loggingT) setV(pc uintptr) Level { - fn := runtime.FuncForPC(pc) - file, _ := fn.FileLine(pc) - // The file is something like /a/b/c/d.go. We want just the d. - if strings.HasSuffix(file, ".go") { - file = file[:len(file)-3] - } - if slash := strings.LastIndex(file, "/"); slash >= 0 { - file = file[slash+1:] - } - for _, filter := range l.vmodule.filter { - if filter.match(file) { - l.vmap[pc] = filter.level - return filter.level - } - } - l.vmap[pc] = 0 - return 0 -} - -// Verbose is a boolean type that implements Infof (like Printf) etc. -// See the documentation of V for more information. -type Verbose bool - -// V reports whether verbosity at the call site is at least the requested level. -// The returned value is a boolean of type Verbose, which implements Info, Infoln -// and Infof. These methods will write to the Info log if called. -// Thus, one may write either -// if glog.V(2) { glog.Info("log this") } -// or -// glog.V(2).Info("log this") -// The second form is shorter but the first is cheaper if logging is off because it does -// not evaluate its arguments. -// -// Whether an individual call to V generates a log record depends on the setting of -// the -v and --vmodule flags; both are off by default. If the level in the call to -// V is at least the value of -v, or of -vmodule for the source file containing the -// call, the V call will log. -func V(level Level) Verbose { - // This function tries hard to be cheap unless there's work to do. - // The fast path is two atomic loads and compares. - - // Here is a cheap but safe test to see if V logging is enabled globally. - if logging.verbosity.get() >= level { - return Verbose(true) - } - - // It's off globally but it vmodule may still be set. - // Here is another cheap but safe test to see if vmodule is enabled. - if atomic.LoadInt32(&logging.filterLength) > 0 { - // Now we need a proper lock to use the logging structure. The pcs field - // is shared so we must lock before accessing it. This is fairly expensive, - // but if V logging is enabled we're slow anyway. - logging.mu.Lock() - defer logging.mu.Unlock() - if runtime.Callers(2, logging.pcs[:]) == 0 { - return Verbose(false) - } - v, ok := logging.vmap[logging.pcs[0]] - if !ok { - v = logging.setV(logging.pcs[0]) - } - return Verbose(v >= level) - } - return Verbose(false) -} - -// Info is equivalent to the global Info function, guarded by the value of v. -// See the documentation of V for usage. -func (v Verbose) Info(args ...interface{}) { - if v { - logging.print(infoLog, args...) - } -} - -// Infoln is equivalent to the global Infoln function, guarded by the value of v. -// See the documentation of V for usage. -func (v Verbose) Infoln(args ...interface{}) { - if v { - logging.println(infoLog, args...) - } -} - -// Infof is equivalent to the global Infof function, guarded by the value of v. -// See the documentation of V for usage. -func (v Verbose) Infof(format string, args ...interface{}) { - if v { - logging.printf(infoLog, format, args...) - } -} - -// Info logs to the INFO log. -// Arguments are handled in the manner of fmt.Print; a newline is appended if missing. -func Info(args ...interface{}) { - logging.print(infoLog, args...) -} - -// InfoDepth acts as Info but uses depth to determine which call frame to log. -// InfoDepth(0, "msg") is the same as Info("msg"). -func InfoDepth(depth int, args ...interface{}) { - logging.printDepth(infoLog, depth, args...) -} - -// Infoln logs to the INFO log. -// Arguments are handled in the manner of fmt.Println; a newline is appended if missing. -func Infoln(args ...interface{}) { - logging.println(infoLog, args...) -} - -// Infof logs to the INFO log. -// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing. -func Infof(format string, args ...interface{}) { - logging.printf(infoLog, format, args...) -} - -// Warning logs to the WARNING and INFO logs. -// Arguments are handled in the manner of fmt.Print; a newline is appended if missing. -func Warning(args ...interface{}) { - logging.print(warningLog, args...) -} - -// WarningDepth acts as Warning but uses depth to determine which call frame to log. -// WarningDepth(0, "msg") is the same as Warning("msg"). -func WarningDepth(depth int, args ...interface{}) { - logging.printDepth(warningLog, depth, args...) -} - -// Warningln logs to the WARNING and INFO logs. -// Arguments are handled in the manner of fmt.Println; a newline is appended if missing. -func Warningln(args ...interface{}) { - logging.println(warningLog, args...) -} - -// Warningf logs to the WARNING and INFO logs. -// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing. -func Warningf(format string, args ...interface{}) { - logging.printf(warningLog, format, args...) -} - -// Error logs to the ERROR, WARNING, and INFO logs. -// Arguments are handled in the manner of fmt.Print; a newline is appended if missing. -func Error(args ...interface{}) { - logging.print(errorLog, args...) -} - -// ErrorDepth acts as Error but uses depth to determine which call frame to log. -// ErrorDepth(0, "msg") is the same as Error("msg"). -func ErrorDepth(depth int, args ...interface{}) { - logging.printDepth(errorLog, depth, args...) -} - -// Errorln logs to the ERROR, WARNING, and INFO logs. -// Arguments are handled in the manner of fmt.Println; a newline is appended if missing. -func Errorln(args ...interface{}) { - logging.println(errorLog, args...) -} - -// Errorf logs to the ERROR, WARNING, and INFO logs. -// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing. -func Errorf(format string, args ...interface{}) { - logging.printf(errorLog, format, args...) -} - -// Fatal logs to the FATAL, ERROR, WARNING, and INFO logs, -// including a stack trace of all running goroutines, then calls os.Exit(255). -// Arguments are handled in the manner of fmt.Print; a newline is appended if missing. -func Fatal(args ...interface{}) { - logging.print(fatalLog, args...) -} - -// FatalDepth acts as Fatal but uses depth to determine which call frame to log. -// FatalDepth(0, "msg") is the same as Fatal("msg"). -func FatalDepth(depth int, args ...interface{}) { - logging.printDepth(fatalLog, depth, args...) -} - -// Fatalln logs to the FATAL, ERROR, WARNING, and INFO logs, -// including a stack trace of all running goroutines, then calls os.Exit(255). -// Arguments are handled in the manner of fmt.Println; a newline is appended if missing. -func Fatalln(args ...interface{}) { - logging.println(fatalLog, args...) -} - -// Fatalf logs to the FATAL, ERROR, WARNING, and INFO logs, -// including a stack trace of all running goroutines, then calls os.Exit(255). -// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing. -func Fatalf(format string, args ...interface{}) { - logging.printf(fatalLog, format, args...) -} - -// fatalNoStacks is non-zero if we are to exit without dumping goroutine stacks. -// It allows Exit and relatives to use the Fatal logs. -var fatalNoStacks uint32 - -// Exit logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1). -// Arguments are handled in the manner of fmt.Print; a newline is appended if missing. -func Exit(args ...interface{}) { - atomic.StoreUint32(&fatalNoStacks, 1) - logging.print(fatalLog, args...) -} - -// ExitDepth acts as Exit but uses depth to determine which call frame to log. -// ExitDepth(0, "msg") is the same as Exit("msg"). -func ExitDepth(depth int, args ...interface{}) { - atomic.StoreUint32(&fatalNoStacks, 1) - logging.printDepth(fatalLog, depth, args...) -} - -// Exitln logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1). -func Exitln(args ...interface{}) { - atomic.StoreUint32(&fatalNoStacks, 1) - logging.println(fatalLog, args...) -} - -// Exitf logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1). -// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing. -func Exitf(format string, args ...interface{}) { - atomic.StoreUint32(&fatalNoStacks, 1) - logging.printf(fatalLog, format, args...) -} diff --git a/vendor/github.com/golang/glog/glog_file.go b/vendor/github.com/golang/glog/glog_file.go deleted file mode 100644 index 65075d28111..00000000000 --- a/vendor/github.com/golang/glog/glog_file.go +++ /dev/null @@ -1,124 +0,0 @@ -// Go support for leveled logs, analogous to https://code.google.com/p/google-glog/ -// -// Copyright 2013 Google Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// File I/O for logs. - -package glog - -import ( - "errors" - "flag" - "fmt" - "os" - "os/user" - "path/filepath" - "strings" - "sync" - "time" -) - -// MaxSize is the maximum size of a log file in bytes. -var MaxSize uint64 = 1024 * 1024 * 1800 - -// logDirs lists the candidate directories for new log files. -var logDirs []string - -// If non-empty, overrides the choice of directory in which to write logs. -// See createLogDirs for the full list of possible destinations. -var logDir = flag.String("log_dir", "", "If non-empty, write log files in this directory") - -func createLogDirs() { - if *logDir != "" { - logDirs = append(logDirs, *logDir) - } - logDirs = append(logDirs, os.TempDir()) -} - -var ( - pid = os.Getpid() - program = filepath.Base(os.Args[0]) - host = "unknownhost" - userName = "unknownuser" -) - -func init() { - h, err := os.Hostname() - if err == nil { - host = shortHostname(h) - } - - current, err := user.Current() - if err == nil { - userName = current.Username - } - - // Sanitize userName since it may contain filepath separators on Windows. - userName = strings.Replace(userName, `\`, "_", -1) -} - -// shortHostname returns its argument, truncating at the first period. -// For instance, given "www.google.com" it returns "www". -func shortHostname(hostname string) string { - if i := strings.Index(hostname, "."); i >= 0 { - return hostname[:i] - } - return hostname -} - -// logName returns a new log file name containing tag, with start time t, and -// the name for the symlink for tag. -func logName(tag string, t time.Time) (name, link string) { - name = fmt.Sprintf("%s.%s.%s.log.%s.%04d%02d%02d-%02d%02d%02d.%d", - program, - host, - userName, - tag, - t.Year(), - t.Month(), - t.Day(), - t.Hour(), - t.Minute(), - t.Second(), - pid) - return name, program + "." + tag -} - -var onceLogDirs sync.Once - -// create creates a new log file and returns the file and its filename, which -// contains tag ("INFO", "FATAL", etc.) and t. If the file is created -// successfully, create also attempts to update the symlink for that tag, ignoring -// errors. -func create(tag string, t time.Time) (f *os.File, filename string, err error) { - onceLogDirs.Do(createLogDirs) - if len(logDirs) == 0 { - return nil, "", errors.New("log: no log dirs") - } - name, link := logName(tag, t) - var lastErr error - for _, dir := range logDirs { - fname := filepath.Join(dir, name) - f, err := os.Create(fname) - if err == nil { - symlink := filepath.Join(dir, link) - os.Remove(symlink) // ignore err - os.Symlink(name, symlink) // ignore err - return f, fname, nil - } - lastErr = err - } - return nil, "", fmt.Errorf("log: cannot create log: %v", lastErr) -} diff --git a/vendor/github.com/golang/groupcache/lru/lru.go b/vendor/github.com/golang/groupcache/lru/lru.go index 532cc45e6dc..eac1c7664f9 100644 --- a/vendor/github.com/golang/groupcache/lru/lru.go +++ b/vendor/github.com/golang/groupcache/lru/lru.go @@ -25,7 +25,7 @@ type Cache struct { // an item is evicted. Zero means no limit. MaxEntries int - // OnEvicted optionally specificies a callback function to be + // OnEvicted optionally specifies a callback function to be // executed when an entry is purged from the cache. OnEvicted func(key Key, value interface{}) diff --git a/vendor/github.com/google/uuid/go.mod b/vendor/github.com/google/uuid/go.mod new file mode 100644 index 00000000000..fc84cd79d4c --- /dev/null +++ b/vendor/github.com/google/uuid/go.mod @@ -0,0 +1 @@ +module github.com/google/uuid diff --git a/vendor/github.com/google/uuid/uuid.go b/vendor/github.com/google/uuid/uuid.go index 7f3643fe9a6..524404cc522 100644 --- a/vendor/github.com/google/uuid/uuid.go +++ b/vendor/github.com/google/uuid/uuid.go @@ -1,4 +1,4 @@ -// Copyright 2016 Google Inc. All rights reserved. +// Copyright 2018 Google Inc. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. @@ -35,20 +35,43 @@ const ( var rander = rand.Reader // random function -// Parse decodes s into a UUID or returns an error. Both the UUID form of -// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and -// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded. +// Parse decodes s into a UUID or returns an error. Both the standard UUID +// forms of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and +// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded as well as the +// Microsoft encoding {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} and the raw hex +// encoding: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx. func Parse(s string) (UUID, error) { var uuid UUID - if len(s) != 36 { - if len(s) != 36+9 { - return uuid, fmt.Errorf("invalid UUID length: %d", len(s)) - } + switch len(s) { + // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx + case 36: + + // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx + case 36 + 9: if strings.ToLower(s[:9]) != "urn:uuid:" { return uuid, fmt.Errorf("invalid urn prefix: %q", s[:9]) } s = s[9:] + + // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} + case 36 + 2: + s = s[1:] + + // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + case 32: + var ok bool + for i := range uuid { + uuid[i], ok = xtob(s[i*2], s[i*2+1]) + if !ok { + return uuid, errors.New("invalid UUID format") + } + } + return uuid, nil + default: + return uuid, fmt.Errorf("invalid UUID length: %d", len(s)) } + // s is now at least 36 bytes long + // it must be of the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' { return uuid, errors.New("invalid UUID format") } @@ -70,15 +93,29 @@ func Parse(s string) (UUID, error) { // ParseBytes is like Parse, except it parses a byte slice instead of a string. func ParseBytes(b []byte) (UUID, error) { var uuid UUID - if len(b) != 36 { - if len(b) != 36+9 { - return uuid, fmt.Errorf("invalid UUID length: %d", len(b)) - } + switch len(b) { + case 36: // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx + case 36 + 9: // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx if !bytes.Equal(bytes.ToLower(b[:9]), []byte("urn:uuid:")) { return uuid, fmt.Errorf("invalid urn prefix: %q", b[:9]) } b = b[9:] + case 36 + 2: // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} + b = b[1:] + case 32: // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + var ok bool + for i := 0; i < 32; i += 2 { + uuid[i/2], ok = xtob(b[i], b[i+1]) + if !ok { + return uuid, errors.New("invalid UUID format") + } + } + return uuid, nil + default: + return uuid, fmt.Errorf("invalid UUID length: %d", len(b)) } + // s is now at least 36 bytes long + // it must be of the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx if b[8] != '-' || b[13] != '-' || b[18] != '-' || b[23] != '-' { return uuid, errors.New("invalid UUID format") } @@ -97,6 +134,16 @@ func ParseBytes(b []byte) (UUID, error) { return uuid, nil } +// MustParse is like Parse but panics if the string cannot be parsed. +// It simplifies safe initialization of global variables holding compiled UUIDs. +func MustParse(s string) UUID { + uuid, err := Parse(s) + if err != nil { + panic(`uuid: Parse(` + s + `): ` + err.Error()) + } + return uuid +} + // FromBytes creates a new UUID from a byte slice. Returns an error if the slice // does not have a length of 16. The bytes are copied from the slice. func FromBytes(b []byte) (uuid UUID, err error) { @@ -130,7 +177,7 @@ func (uuid UUID) URN() string { } func encodeHex(dst []byte, uuid UUID) { - hex.Encode(dst[:], uuid[:4]) + hex.Encode(dst, uuid[:4]) dst[8] = '-' hex.Encode(dst[9:13], uuid[4:6]) dst[13] = '-' diff --git a/vendor/github.com/gregjones/httpcache/.travis.yml b/vendor/github.com/gregjones/httpcache/.travis.yml index b5ffbe03d86..597bc9996f0 100644 --- a/vendor/github.com/gregjones/httpcache/.travis.yml +++ b/vendor/github.com/gregjones/httpcache/.travis.yml @@ -1,19 +1,18 @@ sudo: false language: go -go: - - 1.6.x - - 1.7.x - - 1.8.x - - 1.9.x - - master matrix: allow_failures: - go: master fast_finish: true + include: + - go: 1.10.x + - go: 1.11.x + env: GOFMT=1 + - go: master install: - # Do nothing. This is needed to prevent default install action "go get -t -v ./..." from happening here (we want it to happen inside script step). script: - go get -t -v ./... - - diff -u <(echo -n) <(gofmt -d .) + - if test -n "${GOFMT}"; then gofmt -w -s . && git diff --exit-code; fi - go tool vet . - go test -v -race ./... diff --git a/vendor/github.com/gregjones/httpcache/httpcache.go b/vendor/github.com/gregjones/httpcache/httpcache.go index f6a2ec4a53e..b41a63d1ff5 100644 --- a/vendor/github.com/gregjones/httpcache/httpcache.go +++ b/vendor/github.com/gregjones/httpcache/httpcache.go @@ -416,14 +416,14 @@ func canStaleOnError(respHeaders, reqHeaders http.Header) bool { func getEndToEndHeaders(respHeaders http.Header) []string { // These headers are always hop-by-hop hopByHopHeaders := map[string]struct{}{ - "Connection": struct{}{}, - "Keep-Alive": struct{}{}, - "Proxy-Authenticate": struct{}{}, - "Proxy-Authorization": struct{}{}, - "Te": struct{}{}, - "Trailers": struct{}{}, - "Transfer-Encoding": struct{}{}, - "Upgrade": struct{}{}, + "Connection": {}, + "Keep-Alive": {}, + "Proxy-Authenticate": {}, + "Proxy-Authorization": {}, + "Te": {}, + "Trailers": {}, + "Transfer-Encoding": {}, + "Upgrade": {}, } for _, extra := range strings.Split(respHeaders.Get("connection"), ",") { @@ -433,7 +433,7 @@ func getEndToEndHeaders(respHeaders http.Header) []string { } } endToEndHeaders := []string{} - for respHeader, _ := range respHeaders { + for respHeader := range respHeaders { if _, ok := hopByHopHeaders[respHeader]; !ok { endToEndHeaders = append(endToEndHeaders, respHeader) } diff --git a/vendor/github.com/imdario/mergo/README.md b/vendor/github.com/imdario/mergo/README.md index 8b76f1fbf33..02fc81e0626 100644 --- a/vendor/github.com/imdario/mergo/README.md +++ b/vendor/github.com/imdario/mergo/README.md @@ -13,6 +13,7 @@ It is ready for production use. [It is used in several projects by Docker, Googl [![Build Status][1]][2] [![Coverage Status][7]][8] [![Sourcegraph][9]][10] +[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fimdario%2Fmergo.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fimdario%2Fmergo?ref=badge_shield) [1]: https://travis-ci.org/imdario/mergo.png [2]: https://travis-ci.org/imdario/mergo @@ -27,7 +28,7 @@ It is ready for production use. [It is used in several projects by Docker, Googl ### Latest release -[Release v0.3.6](https://github.com/imdario/mergo/releases/tag/v0.3.6). +[Release v0.3.7](https://github.com/imdario/mergo/releases/tag/v0.3.7). ### Important note @@ -217,6 +218,21 @@ If I can help you, you have an idea or you are using Mergo in your projects, don Written by [Dario Castañé](http://dario.im). +## Top Contributors + +[![0](https://sourcerer.io/fame/imdario/imdario/mergo/images/0)](https://sourcerer.io/fame/imdario/imdario/mergo/links/0) +[![1](https://sourcerer.io/fame/imdario/imdario/mergo/images/1)](https://sourcerer.io/fame/imdario/imdario/mergo/links/1) +[![2](https://sourcerer.io/fame/imdario/imdario/mergo/images/2)](https://sourcerer.io/fame/imdario/imdario/mergo/links/2) +[![3](https://sourcerer.io/fame/imdario/imdario/mergo/images/3)](https://sourcerer.io/fame/imdario/imdario/mergo/links/3) +[![4](https://sourcerer.io/fame/imdario/imdario/mergo/images/4)](https://sourcerer.io/fame/imdario/imdario/mergo/links/4) +[![5](https://sourcerer.io/fame/imdario/imdario/mergo/images/5)](https://sourcerer.io/fame/imdario/imdario/mergo/links/5) +[![6](https://sourcerer.io/fame/imdario/imdario/mergo/images/6)](https://sourcerer.io/fame/imdario/imdario/mergo/links/6) +[![7](https://sourcerer.io/fame/imdario/imdario/mergo/images/7)](https://sourcerer.io/fame/imdario/imdario/mergo/links/7) + + ## License [BSD 3-Clause](http://opensource.org/licenses/BSD-3-Clause) license, as [Go language](http://golang.org/LICENSE). + + +[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fimdario%2Fmergo.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fimdario%2Fmergo?ref=badge_large) diff --git a/vendor/github.com/imdario/mergo/map.go b/vendor/github.com/imdario/mergo/map.go index 6ea38e636b6..3f5afa83a13 100644 --- a/vendor/github.com/imdario/mergo/map.go +++ b/vendor/github.com/imdario/mergo/map.go @@ -72,6 +72,7 @@ func deepMap(dst, src reflect.Value, visited map[uintptr]*visit, depth int, conf case reflect.Struct: srcMap := src.Interface().(map[string]interface{}) for key := range srcMap { + config.overwriteWithEmptyValue = true srcValue := srcMap[key] fieldName := changeInitialCase(key, unicode.ToUpper) dstElement := dst.FieldByName(fieldName) diff --git a/vendor/github.com/imdario/mergo/merge.go b/vendor/github.com/imdario/mergo/merge.go index 44f70a89d91..f8de6c54305 100644 --- a/vendor/github.com/imdario/mergo/merge.go +++ b/vendor/github.com/imdario/mergo/merge.go @@ -26,9 +26,10 @@ func hasExportedField(dst reflect.Value) (exported bool) { } type Config struct { - Overwrite bool - AppendSlice bool - Transformers Transformers + Overwrite bool + AppendSlice bool + Transformers Transformers + overwriteWithEmptyValue bool } type Transformers interface { @@ -40,6 +41,8 @@ type Transformers interface { // short circuiting on recursive types. func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, config *Config) (err error) { overwrite := config.Overwrite + overwriteWithEmptySrc := config.overwriteWithEmptyValue + config.overwriteWithEmptyValue = false if !src.IsValid() { return @@ -74,7 +77,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co } } } else { - if dst.CanSet() && !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) { + if dst.CanSet() && (!isEmptyValue(src) || overwriteWithEmptySrc) && (overwrite || isEmptyValue(dst)) { dst.Set(src) } } @@ -125,7 +128,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co dstSlice = reflect.ValueOf(dstElement.Interface()) } - if !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice { + if (!isEmptyValue(src) || overwriteWithEmptySrc) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice { dstSlice = srcSlice } else if config.AppendSlice { if srcSlice.Type() != dstSlice.Type() { @@ -136,7 +139,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co dst.SetMapIndex(key, dstSlice) } } - if dstElement.IsValid() && reflect.TypeOf(srcElement.Interface()).Kind() == reflect.Map { + if dstElement.IsValid() && !isEmptyValue(dstElement) && (reflect.TypeOf(srcElement.Interface()).Kind() == reflect.Map || reflect.TypeOf(srcElement.Interface()).Kind() == reflect.Slice) { continue } @@ -151,7 +154,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co if !dst.CanSet() { break } - if !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice { + if (!isEmptyValue(src) || overwriteWithEmptySrc) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice { dst.Set(src) } else if config.AppendSlice { if src.Type() != dst.Type() { @@ -191,7 +194,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co return } default: - if dst.CanSet() && !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) { + if dst.CanSet() && (!isEmptyValue(src) || overwriteWithEmptySrc) && (overwrite || isEmptyValue(dst)) { dst.Set(src) } } diff --git a/vendor/github.com/kr/fs/Readme b/vendor/github.com/kr/fs/Readme deleted file mode 100644 index c95e13fc84f..00000000000 --- a/vendor/github.com/kr/fs/Readme +++ /dev/null @@ -1,3 +0,0 @@ -Filesystem Package - -http://godoc.org/github.com/kr/fs diff --git a/vendor/github.com/kr/fs/filesystem.go b/vendor/github.com/kr/fs/filesystem.go deleted file mode 100644 index f1c4805fbdb..00000000000 --- a/vendor/github.com/kr/fs/filesystem.go +++ /dev/null @@ -1,36 +0,0 @@ -package fs - -import ( - "io/ioutil" - "os" - "path/filepath" -) - -// FileSystem defines the methods of an abstract filesystem. -type FileSystem interface { - - // ReadDir reads the directory named by dirname and returns a - // list of directory entries. - ReadDir(dirname string) ([]os.FileInfo, error) - - // Lstat returns a FileInfo describing the named file. If the file is a - // symbolic link, the returned FileInfo describes the symbolic link. Lstat - // makes no attempt to follow the link. - Lstat(name string) (os.FileInfo, error) - - // Join joins any number of path elements into a single path, adding a - // separator if necessary. The result is Cleaned; in particular, all - // empty strings are ignored. - // - // The separator is FileSystem specific. - Join(elem ...string) string -} - -// fs represents a FileSystem provided by the os package. -type fs struct{} - -func (f *fs) ReadDir(dirname string) ([]os.FileInfo, error) { return ioutil.ReadDir(dirname) } - -func (f *fs) Lstat(name string) (os.FileInfo, error) { return os.Lstat(name) } - -func (f *fs) Join(elem ...string) string { return filepath.Join(elem...) } diff --git a/vendor/github.com/kr/fs/go.mod b/vendor/github.com/kr/fs/go.mod deleted file mode 100644 index 7c206e04c8a..00000000000 --- a/vendor/github.com/kr/fs/go.mod +++ /dev/null @@ -1 +0,0 @@ -module "github.com/kr/fs" diff --git a/vendor/github.com/kr/fs/walk.go b/vendor/github.com/kr/fs/walk.go deleted file mode 100644 index 6ffa1e0b246..00000000000 --- a/vendor/github.com/kr/fs/walk.go +++ /dev/null @@ -1,95 +0,0 @@ -// Package fs provides filesystem-related functions. -package fs - -import ( - "os" -) - -// Walker provides a convenient interface for iterating over the -// descendants of a filesystem path. -// Successive calls to the Step method will step through each -// file or directory in the tree, including the root. The files -// are walked in lexical order, which makes the output deterministic -// but means that for very large directories Walker can be inefficient. -// Walker does not follow symbolic links. -type Walker struct { - fs FileSystem - cur item - stack []item - descend bool -} - -type item struct { - path string - info os.FileInfo - err error -} - -// Walk returns a new Walker rooted at root. -func Walk(root string) *Walker { - return WalkFS(root, new(fs)) -} - -// WalkFS returns a new Walker rooted at root on the FileSystem fs. -func WalkFS(root string, fs FileSystem) *Walker { - info, err := fs.Lstat(root) - return &Walker{ - fs: fs, - stack: []item{{root, info, err}}, - } -} - -// Step advances the Walker to the next file or directory, -// which will then be available through the Path, Stat, -// and Err methods. -// It returns false when the walk stops at the end of the tree. -func (w *Walker) Step() bool { - if w.descend && w.cur.err == nil && w.cur.info.IsDir() { - list, err := w.fs.ReadDir(w.cur.path) - if err != nil { - w.cur.err = err - w.stack = append(w.stack, w.cur) - } else { - for i := len(list) - 1; i >= 0; i-- { - path := w.fs.Join(w.cur.path, list[i].Name()) - w.stack = append(w.stack, item{path, list[i], nil}) - } - } - } - - if len(w.stack) == 0 { - return false - } - i := len(w.stack) - 1 - w.cur = w.stack[i] - w.stack = w.stack[:i] - w.descend = true - return true -} - -// Path returns the path to the most recent file or directory -// visited by a call to Step. It contains the argument to Walk -// as a prefix; that is, if Walk is called with "dir", which is -// a directory containing the file "a", Path will return "dir/a". -func (w *Walker) Path() string { - return w.cur.path -} - -// Stat returns info for the most recent file or directory -// visited by a call to Step. -func (w *Walker) Stat() os.FileInfo { - return w.cur.info -} - -// Err returns the error, if any, for the most recent attempt -// by Step to visit a file or directory. If a directory has -// an error, w will not descend into that directory. -func (w *Walker) Err() error { - return w.cur.err -} - -// SkipDir causes the currently visited directory to be skipped. -// If w is not on a directory, SkipDir has no effect. -func (w *Walker) SkipDir() { - w.descend = false -} diff --git a/vendor/github.com/mitchellh/go-homedir/homedir.go b/vendor/github.com/mitchellh/go-homedir/homedir.go index fb87bef94f3..25378537ead 100644 --- a/vendor/github.com/mitchellh/go-homedir/homedir.go +++ b/vendor/github.com/mitchellh/go-homedir/homedir.go @@ -76,6 +76,16 @@ func Expand(path string) (string, error) { return filepath.Join(dir, path[1:]), nil } +// Reset clears the cache, forcing the next call to Dir to re-detect +// the home directory. This generally never has to be called, but can be +// useful in tests if you're modifying the home directory via the HOME +// env var or something. +func Reset() { + cacheLock.Lock() + defer cacheLock.Unlock() + homedirCache = "" +} + func dirUnix() (string, error) { homeEnv := "HOME" if runtime.GOOS == "plan9" { diff --git a/vendor/github.com/onsi/ginkgo/.gitignore b/vendor/github.com/onsi/ginkgo/.gitignore index 18793c248aa..b9f9659d29a 100644 --- a/vendor/github.com/onsi/ginkgo/.gitignore +++ b/vendor/github.com/onsi/ginkgo/.gitignore @@ -4,4 +4,4 @@ tmp/**/* *.coverprofile .vscode .idea/ -*.log \ No newline at end of file +*.log diff --git a/vendor/github.com/onsi/ginkgo/.travis.yml b/vendor/github.com/onsi/ginkgo/.travis.yml index 7ad39b78f69..3900878bd20 100644 --- a/vendor/github.com/onsi/ginkgo/.travis.yml +++ b/vendor/github.com/onsi/ginkgo/.travis.yml @@ -5,6 +5,7 @@ go: - 1.8.x - 1.9.x - 1.10.x + - 1.11.x install: - go get -v -t ./... diff --git a/vendor/github.com/onsi/ginkgo/CHANGELOG.md b/vendor/github.com/onsi/ginkgo/CHANGELOG.md index 32370206bfc..d7d797017ab 100644 --- a/vendor/github.com/onsi/ginkgo/CHANGELOG.md +++ b/vendor/github.com/onsi/ginkgo/CHANGELOG.md @@ -1,3 +1,12 @@ +## 1.7.0 + +### New Features +- Add JustAfterEach (#484) [0d4f080] + +### Fixes +- Correctly round suite time in junit reporter [2445fc1] +- Avoid using -i argument to go test for Golang 1.10+ [46bbc26] + ## 1.6.0 ### New Features diff --git a/vendor/github.com/onsi/ginkgo/config/config.go b/vendor/github.com/onsi/ginkgo/config/config.go index d4ed1fa5741..5e509313c3c 100644 --- a/vendor/github.com/onsi/ginkgo/config/config.go +++ b/vendor/github.com/onsi/ginkgo/config/config.go @@ -20,7 +20,7 @@ import ( "fmt" ) -const VERSION = "1.6.0" +const VERSION = "1.7.0" type GinkgoConfigType struct { RandomSeed int64 diff --git a/vendor/github.com/onsi/ginkgo/ginkgo_dsl.go b/vendor/github.com/onsi/ginkgo/ginkgo_dsl.go index 158acdd5e73..5aa96b4d935 100644 --- a/vendor/github.com/onsi/ginkgo/ginkgo_dsl.go +++ b/vendor/github.com/onsi/ginkgo/ginkgo_dsl.go @@ -590,6 +590,16 @@ func JustBeforeEach(body interface{}, timeout ...float64) bool { return true } +//JustAfterEach blocks are run after It blocks but *before* all AfterEach blocks. For more details, +//read the [documentation](http://onsi.github.io/ginkgo/#separating_creation_and_configuration_) +// +//Like It blocks, JustAfterEach blocks can be made asynchronous by providing a body function that accepts +//a Done channel +func JustAfterEach(body interface{}, timeout ...float64) bool { + globalSuite.PushJustAfterEachNode(body, codelocation.New(1), parseTimeout(timeout...)) + return true +} + //AfterEach blocks are run after It blocks. When multiple AfterEach blocks are defined in nested //Describe and Context blocks the innermost AfterEach blocks are run first. // diff --git a/vendor/github.com/onsi/ginkgo/internal/leafnodes/setup_nodes.go b/vendor/github.com/onsi/ginkgo/internal/leafnodes/setup_nodes.go index b4654cd2994..e3e9cb7c588 100644 --- a/vendor/github.com/onsi/ginkgo/internal/leafnodes/setup_nodes.go +++ b/vendor/github.com/onsi/ginkgo/internal/leafnodes/setup_nodes.go @@ -40,3 +40,9 @@ func NewJustBeforeEachNode(body interface{}, codeLocation types.CodeLocation, ti runner: newRunner(body, codeLocation, timeout, failer, types.SpecComponentTypeJustBeforeEach, componentIndex), } } + +func NewJustAfterEachNode(body interface{}, codeLocation types.CodeLocation, timeout time.Duration, failer *failer.Failer, componentIndex int) *SetupNode { + return &SetupNode{ + runner: newRunner(body, codeLocation, timeout, failer, types.SpecComponentTypeJustAfterEach, componentIndex), + } +} diff --git a/vendor/github.com/onsi/ginkgo/internal/spec/spec.go b/vendor/github.com/onsi/ginkgo/internal/spec/spec.go index 77b23a4c773..7fd68ee8e03 100644 --- a/vendor/github.com/onsi/ginkgo/internal/spec/spec.go +++ b/vendor/github.com/onsi/ginkgo/internal/spec/spec.go @@ -161,6 +161,18 @@ func (spec *Spec) runSample(sample int, writer io.Writer) { innerMostContainerIndexToUnwind := -1 defer func() { + for i := innerMostContainerIndexToUnwind; i >= 0; i-- { + container := spec.containers[i] + for _, justAfterEach := range container.SetupNodesOfType(types.SpecComponentTypeJustAfterEach) { + spec.announceSetupNode(writer, "JustAfterEach", container, justAfterEach) + justAfterEachState, justAfterEachFailure := justAfterEach.Run() + if justAfterEachState != types.SpecStatePassed && spec.state == types.SpecStatePassed { + spec.state = justAfterEachState + spec.failure = justAfterEachFailure + } + } + } + for i := innerMostContainerIndexToUnwind; i >= 0; i-- { container := spec.containers[i] for _, afterEach := range container.SetupNodesOfType(types.SpecComponentTypeAfterEach) { diff --git a/vendor/github.com/onsi/ginkgo/internal/suite/suite.go b/vendor/github.com/onsi/ginkgo/internal/suite/suite.go index f311e9a0d65..3104bbc88c7 100644 --- a/vendor/github.com/onsi/ginkgo/internal/suite/suite.go +++ b/vendor/github.com/onsi/ginkgo/internal/suite/suite.go @@ -175,6 +175,13 @@ func (suite *Suite) PushJustBeforeEachNode(body interface{}, codeLocation types. suite.currentContainer.PushSetupNode(leafnodes.NewJustBeforeEachNode(body, codeLocation, timeout, suite.failer, suite.containerIndex)) } +func (suite *Suite) PushJustAfterEachNode(body interface{}, codeLocation types.CodeLocation, timeout time.Duration) { + if suite.running { + suite.failer.Fail("You may only call JustAfterEach from within a Describe or Context", codeLocation) + } + suite.currentContainer.PushSetupNode(leafnodes.NewJustAfterEachNode(body, codeLocation, timeout, suite.failer, suite.containerIndex)) +} + func (suite *Suite) PushAfterEachNode(body interface{}, codeLocation types.CodeLocation, timeout time.Duration) { if suite.running { suite.failer.Fail("You may only call AfterEach from within a Describe, Context or When", codeLocation) diff --git a/vendor/github.com/onsi/ginkgo/reporters/junit_reporter.go b/vendor/github.com/onsi/ginkgo/reporters/junit_reporter.go index 65b8964e518..2c9f3c79299 100644 --- a/vendor/github.com/onsi/ginkgo/reporters/junit_reporter.go +++ b/vendor/github.com/onsi/ginkgo/reporters/junit_reporter.go @@ -121,7 +121,7 @@ func (reporter *JUnitReporter) SpecDidComplete(specSummary *types.SpecSummary) { func (reporter *JUnitReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) { reporter.suite.Tests = summary.NumberOfSpecsThatWillBeRun - reporter.suite.Time = math.Trunc(summary.RunTime.Seconds() * 1000 / 1000) + reporter.suite.Time = math.Trunc(summary.RunTime.Seconds()*1000) / 1000 reporter.suite.Failures = summary.NumberOfFailedSpecs reporter.suite.Errors = 0 file, err := os.Create(reporter.filename) diff --git a/vendor/github.com/onsi/ginkgo/types/types.go b/vendor/github.com/onsi/ginkgo/types/types.go index baf1bd1c474..0e89521be9a 100644 --- a/vendor/github.com/onsi/ginkgo/types/types.go +++ b/vendor/github.com/onsi/ginkgo/types/types.go @@ -159,6 +159,7 @@ const ( SpecComponentTypeAfterSuite SpecComponentTypeBeforeEach SpecComponentTypeJustBeforeEach + SpecComponentTypeJustAfterEach SpecComponentTypeAfterEach SpecComponentTypeIt SpecComponentTypeMeasure diff --git a/vendor/github.com/onsi/gomega/.travis.yml b/vendor/github.com/onsi/gomega/.travis.yml index 783fb3b483a..4d71367f6fb 100644 --- a/vendor/github.com/onsi/gomega/.travis.yml +++ b/vendor/github.com/onsi/gomega/.travis.yml @@ -1,4 +1,5 @@ language: go + go: - 1.6.x - 1.7.x @@ -7,10 +8,16 @@ go: - 1.10.x - 1.11.x +env: + - GO111MODULE=on + install: - - env GO111MODULE=on go get -v ./... - - env GO111MODULE=on go build ./... + - go get -v ./... + - go build ./... - go get github.com/onsi/ginkgo - go install github.com/onsi/ginkgo/ginkgo -script: env GO111MODULE=on $HOME/gopath/bin/ginkgo -p -r --randomizeAllSpecs --failOnPending --randomizeSuites --race && env GO111MODULE=on go vet +script: | + $HOME/gopath/bin/ginkgo -p -r --randomizeAllSpecs --failOnPending --randomizeSuites --race && + go vet && + [ -z "`gofmt -l -e -s -w .`" ] diff --git a/vendor/github.com/onsi/gomega/CHANGELOG.md b/vendor/github.com/onsi/gomega/CHANGELOG.md index f2aaea4eeef..9153294f75f 100644 --- a/vendor/github.com/onsi/gomega/CHANGELOG.md +++ b/vendor/github.com/onsi/gomega/CHANGELOG.md @@ -1,3 +1,10 @@ +## 1.4.3 + +### Fixes: + +- ensure file name and line numbers are correctly reported for XUnit [6fff58f] +- Fixed matcher for content-type (#305) [69d9b43] + ## 1.4.2 ### Fixes: diff --git a/vendor/github.com/onsi/gomega/gomega_dsl.go b/vendor/github.com/onsi/gomega/gomega_dsl.go index 51d7872fc64..471f691a62a 100644 --- a/vendor/github.com/onsi/gomega/gomega_dsl.go +++ b/vendor/github.com/onsi/gomega/gomega_dsl.go @@ -24,7 +24,7 @@ import ( "github.com/onsi/gomega/types" ) -const GOMEGA_VERSION = "1.4.2" +const GOMEGA_VERSION = "1.4.3" const nilFailHandlerPanic = `You are trying to make an assertion, but Gomega's fail handler is nil. If you're using Ginkgo then you probably forgot to put your assertion in an It(). @@ -46,12 +46,25 @@ func RegisterFailHandler(handler types.GomegaFailHandler) { globalFailWrapper = nil return } + globalFailWrapper = &types.GomegaFailWrapper{ Fail: handler, TWithHelper: testingtsupport.EmptyTWithHelper{}, } } +func RegisterFailHandlerWithT(t types.TWithHelper, handler types.GomegaFailHandler) { + if handler == nil { + globalFailWrapper = nil + return + } + + globalFailWrapper = &types.GomegaFailWrapper{ + Fail: handler, + TWithHelper: t, + } +} + //RegisterTestingT connects Gomega to Golang's XUnit style //Testing.T tests. It is now deprecated and you should use NewGomegaWithT() instead. // @@ -74,7 +87,12 @@ func RegisterFailHandler(handler types.GomegaFailHandler) { // // (As an aside: Ginkgo gets around this limitation by running parallel tests in different *processes*). func RegisterTestingT(t types.GomegaTestingT) { - RegisterFailHandler(testingtsupport.BuildTestingTGomegaFailWrapper(t).Fail) + tWithHelper, hasHelper := t.(types.TWithHelper) + if !hasHelper { + RegisterFailHandler(testingtsupport.BuildTestingTGomegaFailWrapper(t).Fail) + return + } + RegisterFailHandlerWithT(tWithHelper, testingtsupport.BuildTestingTGomegaFailWrapper(t).Fail) } //InterceptGomegaHandlers runs a given callback and returns an array of diff --git a/vendor/github.com/onsi/gomega/matchers/be_closed_matcher.go b/vendor/github.com/onsi/gomega/matchers/be_closed_matcher.go index ed6f69288e2..80c9c8bb17b 100644 --- a/vendor/github.com/onsi/gomega/matchers/be_closed_matcher.go +++ b/vendor/github.com/onsi/gomega/matchers/be_closed_matcher.go @@ -23,8 +23,8 @@ func (matcher *BeClosedMatcher) Match(actual interface{}) (success bool, err err } winnerIndex, _, open := reflect.Select([]reflect.SelectCase{ - reflect.SelectCase{Dir: reflect.SelectRecv, Chan: channelValue}, - reflect.SelectCase{Dir: reflect.SelectDefault}, + {Dir: reflect.SelectRecv, Chan: channelValue}, + {Dir: reflect.SelectDefault}, }) var closed bool diff --git a/vendor/github.com/onsi/gomega/matchers/be_sent_matcher.go b/vendor/github.com/onsi/gomega/matchers/be_sent_matcher.go index d7c32233ec4..302dd1a0ace 100644 --- a/vendor/github.com/onsi/gomega/matchers/be_sent_matcher.go +++ b/vendor/github.com/onsi/gomega/matchers/be_sent_matcher.go @@ -42,8 +42,8 @@ func (matcher *BeSentMatcher) Match(actual interface{}) (success bool, err error }() winnerIndex, _, _ := reflect.Select([]reflect.SelectCase{ - reflect.SelectCase{Dir: reflect.SelectSend, Chan: channelValue, Send: argValue}, - reflect.SelectCase{Dir: reflect.SelectDefault}, + {Dir: reflect.SelectSend, Chan: channelValue, Send: argValue}, + {Dir: reflect.SelectDefault}, }) var didSend bool diff --git a/vendor/github.com/onsi/gomega/matchers/match_yaml_matcher.go b/vendor/github.com/onsi/gomega/matchers/match_yaml_matcher.go index 070d6026076..0c83c2b638a 100644 --- a/vendor/github.com/onsi/gomega/matchers/match_yaml_matcher.go +++ b/vendor/github.com/onsi/gomega/matchers/match_yaml_matcher.go @@ -53,11 +53,11 @@ func normalise(input string) string { var val interface{} err := yaml.Unmarshal([]byte(input), &val) if err != nil { - panic(err) // guarded by Match + panic(err) // unreachable since Match already calls Unmarshal } output, err := yaml.Marshal(val) if err != nil { - panic(err) // guarded by Unmarshal + panic(err) // untested section, unreachable since we Unmarshal above } return strings.TrimSpace(string(output)) } diff --git a/vendor/github.com/onsi/gomega/matchers/receive_matcher.go b/vendor/github.com/onsi/gomega/matchers/receive_matcher.go index fc179916d27..2018a6128cd 100644 --- a/vendor/github.com/onsi/gomega/matchers/receive_matcher.go +++ b/vendor/github.com/onsi/gomega/matchers/receive_matcher.go @@ -39,8 +39,8 @@ func (matcher *ReceiveMatcher) Match(actual interface{}) (success bool, err erro } winnerIndex, value, open := reflect.Select([]reflect.SelectCase{ - reflect.SelectCase{Dir: reflect.SelectRecv, Chan: channelValue}, - reflect.SelectCase{Dir: reflect.SelectDefault}, + {Dir: reflect.SelectRecv, Chan: channelValue}, + {Dir: reflect.SelectDefault}, }) var closed bool diff --git a/vendor/github.com/onsi/gomega/matchers/support/goraph/bipartitegraph/bipartitegraph.go b/vendor/github.com/onsi/gomega/matchers/support/goraph/bipartitegraph/bipartitegraph.go index 81b3771119b..8aaf8759d4d 100644 --- a/vendor/github.com/onsi/gomega/matchers/support/goraph/bipartitegraph/bipartitegraph.go +++ b/vendor/github.com/onsi/gomega/matchers/support/goraph/bipartitegraph/bipartitegraph.go @@ -14,12 +14,12 @@ type BipartiteGraph struct { func NewBipartiteGraph(leftValues, rightValues []interface{}, neighbours func(interface{}, interface{}) (bool, error)) (*BipartiteGraph, error) { left := NodeOrderedSet{} - for i, _ := range leftValues { + for i := range leftValues { left = append(left, Node{Id: i}) } right := NodeOrderedSet{} - for j, _ := range rightValues { + for j := range rightValues { right = append(right, Node{Id: j + len(left)}) } diff --git a/vendor/github.com/pkg/errors/.travis.yml b/vendor/github.com/pkg/errors/.travis.yml index 588ceca183f..d4b92663bac 100644 --- a/vendor/github.com/pkg/errors/.travis.yml +++ b/vendor/github.com/pkg/errors/.travis.yml @@ -1,10 +1,14 @@ language: go go_import_path: github.com/pkg/errors go: - - 1.4.3 - - 1.5.4 - - 1.6.2 - - 1.7.1 + - 1.4.x + - 1.5.x + - 1.6.x + - 1.7.x + - 1.8.x + - 1.9.x + - 1.10.x + - 1.11.x - tip script: diff --git a/vendor/github.com/pkg/errors/README.md b/vendor/github.com/pkg/errors/README.md index 273db3c98ae..6483ba2afb5 100644 --- a/vendor/github.com/pkg/errors/README.md +++ b/vendor/github.com/pkg/errors/README.md @@ -1,4 +1,4 @@ -# errors [![Travis-CI](https://travis-ci.org/pkg/errors.svg)](https://travis-ci.org/pkg/errors) [![AppVeyor](https://ci.appveyor.com/api/projects/status/b98mptawhudj53ep/branch/master?svg=true)](https://ci.appveyor.com/project/davecheney/errors/branch/master) [![GoDoc](https://godoc.org/github.com/pkg/errors?status.svg)](http://godoc.org/github.com/pkg/errors) [![Report card](https://goreportcard.com/badge/github.com/pkg/errors)](https://goreportcard.com/report/github.com/pkg/errors) +# errors [![Travis-CI](https://travis-ci.org/pkg/errors.svg)](https://travis-ci.org/pkg/errors) [![AppVeyor](https://ci.appveyor.com/api/projects/status/b98mptawhudj53ep/branch/master?svg=true)](https://ci.appveyor.com/project/davecheney/errors/branch/master) [![GoDoc](https://godoc.org/github.com/pkg/errors?status.svg)](http://godoc.org/github.com/pkg/errors) [![Report card](https://goreportcard.com/badge/github.com/pkg/errors)](https://goreportcard.com/report/github.com/pkg/errors) [![Sourcegraph](https://sourcegraph.com/github.com/pkg/errors/-/badge.svg)](https://sourcegraph.com/github.com/pkg/errors?badge) Package errors provides simple error handling primitives. @@ -47,6 +47,6 @@ We welcome pull requests, bug fixes and issue reports. With that said, the bar f Before proposing a change, please discuss your change by raising an issue. -## Licence +## License BSD-2-Clause diff --git a/vendor/github.com/pkg/errors/errors.go b/vendor/github.com/pkg/errors/errors.go index 842ee80456d..7421f326ffe 100644 --- a/vendor/github.com/pkg/errors/errors.go +++ b/vendor/github.com/pkg/errors/errors.go @@ -6,7 +6,7 @@ // return err // } // -// which applied recursively up the call stack results in error reports +// which when applied recursively up the call stack results in error reports // without context or debugging information. The errors package allows // programmers to add context to the failure path in their code in a way // that does not destroy the original value of the error. @@ -15,16 +15,17 @@ // // The errors.Wrap function returns a new error that adds context to the // original error by recording a stack trace at the point Wrap is called, -// and the supplied message. For example +// together with the supplied message. For example // // _, err := ioutil.ReadAll(r) // if err != nil { // return errors.Wrap(err, "read failed") // } // -// If additional control is required the errors.WithStack and errors.WithMessage -// functions destructure errors.Wrap into its component operations of annotating -// an error with a stack trace and an a message, respectively. +// If additional control is required, the errors.WithStack and +// errors.WithMessage functions destructure errors.Wrap into its component +// operations: annotating an error with a stack trace and with a message, +// respectively. // // Retrieving the cause of an error // @@ -38,7 +39,7 @@ // } // // can be inspected by errors.Cause. errors.Cause will recursively retrieve -// the topmost error which does not implement causer, which is assumed to be +// the topmost error that does not implement causer, which is assumed to be // the original cause. For example: // // switch err := errors.Cause(err).(type) { @@ -48,16 +49,16 @@ // // unknown error // } // -// causer interface is not exported by this package, but is considered a part -// of stable public API. +// Although the causer interface is not exported by this package, it is +// considered a part of its stable public interface. // // Formatted printing of errors // // All error values returned from this package implement fmt.Formatter and can -// be formatted by the fmt package. The following verbs are supported +// be formatted by the fmt package. The following verbs are supported: // // %s print the error. If the error has a Cause it will be -// printed recursively +// printed recursively. // %v see %s // %+v extended format. Each Frame of the error's StackTrace will // be printed in detail. @@ -65,13 +66,13 @@ // Retrieving the stack trace of an error or wrapper // // New, Errorf, Wrap, and Wrapf record a stack trace at the point they are -// invoked. This information can be retrieved with the following interface. +// invoked. This information can be retrieved with the following interface: // // type stackTracer interface { // StackTrace() errors.StackTrace // } // -// Where errors.StackTrace is defined as +// The returned errors.StackTrace type is defined as // // type StackTrace []Frame // @@ -85,8 +86,8 @@ // } // } // -// stackTracer interface is not exported by this package, but is considered a part -// of stable public API. +// Although the stackTracer interface is not exported by this package, it is +// considered a part of its stable public interface. // // See the documentation for Frame.Format for more details. package errors @@ -192,7 +193,7 @@ func Wrap(err error, message string) error { } // Wrapf returns an error annotating err with a stack trace -// at the point Wrapf is call, and the format specifier. +// at the point Wrapf is called, and the format specifier. // If err is nil, Wrapf returns nil. func Wrapf(err error, format string, args ...interface{}) error { if err == nil { @@ -220,6 +221,18 @@ func WithMessage(err error, message string) error { } } +// WithMessagef annotates err with the format specifier. +// If err is nil, WithMessagef returns nil. +func WithMessagef(err error, format string, args ...interface{}) error { + if err == nil { + return nil + } + return &withMessage{ + cause: err, + msg: fmt.Sprintf(format, args...), + } +} + type withMessage struct { cause error msg string diff --git a/vendor/github.com/pkg/errors/stack.go b/vendor/github.com/pkg/errors/stack.go index 6b1f2891a5a..2874a048cf3 100644 --- a/vendor/github.com/pkg/errors/stack.go +++ b/vendor/github.com/pkg/errors/stack.go @@ -46,7 +46,8 @@ func (f Frame) line() int { // // Format accepts flags that alter the printing of some verbs, as follows: // -// %+s path of source file relative to the compile time GOPATH +// %+s function name and path of source file relative to the compile time +// GOPATH separated by \n\t (\n\t) // %+v equivalent to %+s:%d func (f Frame) Format(s fmt.State, verb rune) { switch verb { @@ -79,6 +80,14 @@ func (f Frame) Format(s fmt.State, verb rune) { // StackTrace is stack of Frames from innermost (newest) to outermost (oldest). type StackTrace []Frame +// Format formats the stack of Frames according to the fmt.Formatter interface. +// +// %s lists source files for each Frame in the stack +// %v lists the source file and line number for each Frame in the stack +// +// Format accepts flags that alter the printing of some verbs, as follows: +// +// %+v Prints filename, function, and line number for each Frame in the stack. func (st StackTrace) Format(s fmt.State, verb rune) { switch verb { case 'v': @@ -136,43 +145,3 @@ func funcname(name string) string { i = strings.Index(name, ".") return name[i+1:] } - -func trimGOPATH(name, file string) string { - // Here we want to get the source file path relative to the compile time - // GOPATH. As of Go 1.6.x there is no direct way to know the compiled - // GOPATH at runtime, but we can infer the number of path segments in the - // GOPATH. We note that fn.Name() returns the function name qualified by - // the import path, which does not include the GOPATH. Thus we can trim - // segments from the beginning of the file path until the number of path - // separators remaining is one more than the number of path separators in - // the function name. For example, given: - // - // GOPATH /home/user - // file /home/user/src/pkg/sub/file.go - // fn.Name() pkg/sub.Type.Method - // - // We want to produce: - // - // pkg/sub/file.go - // - // From this we can easily see that fn.Name() has one less path separator - // than our desired output. We count separators from the end of the file - // path until it finds two more than in the function name and then move - // one character forward to preserve the initial path segment without a - // leading separator. - const sep = "/" - goal := strings.Count(name, sep) + 2 - i := len(file) - for n := 0; n < goal; n++ { - i = strings.LastIndex(file[:i], sep) - if i == -1 { - // not enough separators found, set i so that the slice expression - // below leaves file unmodified - i = -len(sep) - break - } - } - // get back to 0 or trim the leading separator - file = file[i+len(sep):] - return file -} diff --git a/vendor/github.com/pkg/sftp/.gitignore b/vendor/github.com/pkg/sftp/.gitignore deleted file mode 100644 index e1ec837c3f5..00000000000 --- a/vendor/github.com/pkg/sftp/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -.*.swo -.*.swp - -server_standalone/server_standalone - -examples/*/id_rsa -examples/*/id_rsa.pub diff --git a/vendor/github.com/pkg/sftp/.travis.yml b/vendor/github.com/pkg/sftp/.travis.yml deleted file mode 100644 index 44cd0e2eea5..00000000000 --- a/vendor/github.com/pkg/sftp/.travis.yml +++ /dev/null @@ -1,38 +0,0 @@ -language: go -go_import_path: github.com/pkg/sftp - -# current and previous stable releases, plus tip -# remember to exclude previous and tip for macs below -go: - - 1.10.x - - 1.11.x - - tip - -os: - - linux - - osx - -matrix: - exclude: - - os: osx - go: 1.10.x - - os: osx - go: tip - -sudo: false - -addons: - ssh_known_hosts: - - bitbucket.org - -install: - - go get -t -v ./... - - ssh-keygen -t rsa -q -P "" -f $HOME/.ssh/id_rsa - -script: - - go test -integration -v ./... - - go test -testserver -v ./... - - go test -integration -testserver -v ./... - - go test -race -integration -v ./... - - go test -race -testserver -v ./... - - go test -race -integration -testserver -v ./... diff --git a/vendor/github.com/pkg/sftp/BUILD.bazel b/vendor/github.com/pkg/sftp/BUILD.bazel deleted file mode 100644 index 23ccc5a958b..00000000000 --- a/vendor/github.com/pkg/sftp/BUILD.bazel +++ /dev/null @@ -1,41 +0,0 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library") - -go_library( - name = "go_default_library", - srcs = [ - "attrs.go", - "attrs_stubs.go", - "attrs_unix.go", - "client.go", - "conn.go", - "match.go", - "packet.go", - "packet-manager.go", - "packet-typing.go", - "release.go", - "request.go", - "request-attrs.go", - "request-errors.go", - "request-example.go", - "request-interfaces.go", - "request-server.go", - "request-unix.go", - "request_windows.go", - "server.go", - "server_statvfs_darwin.go", - "server_statvfs_impl.go", - "server_statvfs_linux.go", - "server_statvfs_stubs.go", - "server_stubs.go", - "server_unix.go", - "sftp.go", - ], - importmap = "sigs.k8s.io/cluster-api-provider-azure/vendor/github.com/pkg/sftp", - importpath = "github.com/pkg/sftp", - visibility = ["//visibility:public"], - deps = [ - "//vendor/github.com/kr/fs:go_default_library", - "//vendor/github.com/pkg/errors:go_default_library", - "//vendor/golang.org/x/crypto/ssh:go_default_library", - ], -) diff --git a/vendor/github.com/pkg/sftp/CONTRIBUTORS b/vendor/github.com/pkg/sftp/CONTRIBUTORS deleted file mode 100644 index 5c7196ae6a7..00000000000 --- a/vendor/github.com/pkg/sftp/CONTRIBUTORS +++ /dev/null @@ -1,3 +0,0 @@ -Dave Cheney -Saulius Gurklys -John Eikenberry diff --git a/vendor/github.com/pkg/sftp/LICENSE b/vendor/github.com/pkg/sftp/LICENSE deleted file mode 100644 index b7b53921e98..00000000000 --- a/vendor/github.com/pkg/sftp/LICENSE +++ /dev/null @@ -1,9 +0,0 @@ -Copyright (c) 2013, Dave Cheney -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/github.com/pkg/sftp/README.md b/vendor/github.com/pkg/sftp/README.md deleted file mode 100644 index 1fb700c41a4..00000000000 --- a/vendor/github.com/pkg/sftp/README.md +++ /dev/null @@ -1,44 +0,0 @@ -sftp ----- - -The `sftp` package provides support for file system operations on remote ssh -servers using the SFTP subsystem. It also implements an SFTP server for serving -files from the filesystem. - -[![UNIX Build Status](https://travis-ci.org/pkg/sftp.svg?branch=master)](https://travis-ci.org/pkg/sftp) [![GoDoc](http://godoc.org/github.com/pkg/sftp?status.svg)](http://godoc.org/github.com/pkg/sftp) - -usage and examples ------------------- - -See [godoc.org/github.com/pkg/sftp](http://godoc.org/github.com/pkg/sftp) for -examples and usage. - -The basic operation of the package mirrors the facilities of the -[os](http://golang.org/pkg/os) package. - -The Walker interface for directory traversal is heavily inspired by Keith -Rarick's [fs](http://godoc.org/github.com/kr/fs) package. - -roadmap -------- - - * There is way too much duplication in the Client methods. If there was an - unmarshal(interface{}) method this would reduce a heap of the duplication. - -contributing ------------- - -We welcome pull requests, bug fixes and issue reports. - -Before proposing a large change, first please discuss your change by raising an -issue. - -For API/code bugs, please include a small, self contained code example to -reproduce the issue. For pull requests, remember test coverage. - -We try to handle issues and pull requests with a 0 open philosophy. That means -we will try to address the submission as soon as possible and will work toward -a resolution. If progress can no longer be made (eg. unreproducible bug) or -stops (eg. unresponsive submitter), we will close the bug. - -Thanks. diff --git a/vendor/github.com/pkg/sftp/attrs.go b/vendor/github.com/pkg/sftp/attrs.go deleted file mode 100644 index 335bcc284de..00000000000 --- a/vendor/github.com/pkg/sftp/attrs.go +++ /dev/null @@ -1,243 +0,0 @@ -package sftp - -// ssh_FXP_ATTRS support -// see http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02#section-5 - -import ( - "os" - "syscall" - "time" -) - -const ( - ssh_FILEXFER_ATTR_SIZE = 0x00000001 - ssh_FILEXFER_ATTR_UIDGID = 0x00000002 - ssh_FILEXFER_ATTR_PERMISSIONS = 0x00000004 - ssh_FILEXFER_ATTR_ACMODTIME = 0x00000008 - ssh_FILEXFER_ATTR_EXTENDED = 0x80000000 -) - -// fileInfo is an artificial type designed to satisfy os.FileInfo. -type fileInfo struct { - name string - size int64 - mode os.FileMode - mtime time.Time - sys interface{} -} - -// Name returns the base name of the file. -func (fi *fileInfo) Name() string { return fi.name } - -// Size returns the length in bytes for regular files; system-dependent for others. -func (fi *fileInfo) Size() int64 { return fi.size } - -// Mode returns file mode bits. -func (fi *fileInfo) Mode() os.FileMode { return fi.mode } - -// ModTime returns the last modification time of the file. -func (fi *fileInfo) ModTime() time.Time { return fi.mtime } - -// IsDir returns true if the file is a directory. -func (fi *fileInfo) IsDir() bool { return fi.Mode().IsDir() } - -func (fi *fileInfo) Sys() interface{} { return fi.sys } - -// FileStat holds the original unmarshalled values from a call to READDIR or -// *STAT. It is exported for the purposes of accessing the raw values via -// os.FileInfo.Sys(). It is also used server side to store the unmarshalled -// values for SetStat. -type FileStat struct { - Size uint64 - Mode uint32 - Mtime uint32 - Atime uint32 - UID uint32 - GID uint32 - Extended []StatExtended -} - -// StatExtended contains additional, extended information for a FileStat. -type StatExtended struct { - ExtType string - ExtData string -} - -func fileInfoFromStat(st *FileStat, name string) os.FileInfo { - fs := &fileInfo{ - name: name, - size: int64(st.Size), - mode: toFileMode(st.Mode), - mtime: time.Unix(int64(st.Mtime), 0), - sys: st, - } - return fs -} - -func fileStatFromInfo(fi os.FileInfo) (uint32, FileStat) { - mtime := fi.ModTime().Unix() - atime := mtime - var flags uint32 = ssh_FILEXFER_ATTR_SIZE | - ssh_FILEXFER_ATTR_PERMISSIONS | - ssh_FILEXFER_ATTR_ACMODTIME - - fileStat := FileStat{ - Size: uint64(fi.Size()), - Mode: fromFileMode(fi.Mode()), - Mtime: uint32(mtime), - Atime: uint32(atime), - } - - // os specific file stat decoding - fileStatFromInfoOs(fi, &flags, &fileStat) - - return flags, fileStat -} - -func unmarshalAttrs(b []byte) (*FileStat, []byte) { - flags, b := unmarshalUint32(b) - return getFileStat(flags, b) -} - -func getFileStat(flags uint32, b []byte) (*FileStat, []byte) { - var fs FileStat - if flags&ssh_FILEXFER_ATTR_SIZE == ssh_FILEXFER_ATTR_SIZE { - fs.Size, b = unmarshalUint64(b) - } - if flags&ssh_FILEXFER_ATTR_UIDGID == ssh_FILEXFER_ATTR_UIDGID { - fs.UID, b = unmarshalUint32(b) - } - if flags&ssh_FILEXFER_ATTR_UIDGID == ssh_FILEXFER_ATTR_UIDGID { - fs.GID, b = unmarshalUint32(b) - } - if flags&ssh_FILEXFER_ATTR_PERMISSIONS == ssh_FILEXFER_ATTR_PERMISSIONS { - fs.Mode, b = unmarshalUint32(b) - } - if flags&ssh_FILEXFER_ATTR_ACMODTIME == ssh_FILEXFER_ATTR_ACMODTIME { - fs.Atime, b = unmarshalUint32(b) - fs.Mtime, b = unmarshalUint32(b) - } - if flags&ssh_FILEXFER_ATTR_EXTENDED == ssh_FILEXFER_ATTR_EXTENDED { - var count uint32 - count, b = unmarshalUint32(b) - ext := make([]StatExtended, count) - for i := uint32(0); i < count; i++ { - var typ string - var data string - typ, b = unmarshalString(b) - data, b = unmarshalString(b) - ext[i] = StatExtended{typ, data} - } - fs.Extended = ext - } - return &fs, b -} - -func marshalFileInfo(b []byte, fi os.FileInfo) []byte { - // attributes variable struct, and also variable per protocol version - // spec version 3 attributes: - // uint32 flags - // uint64 size present only if flag SSH_FILEXFER_ATTR_SIZE - // uint32 uid present only if flag SSH_FILEXFER_ATTR_UIDGID - // uint32 gid present only if flag SSH_FILEXFER_ATTR_UIDGID - // uint32 permissions present only if flag SSH_FILEXFER_ATTR_PERMISSIONS - // uint32 atime present only if flag SSH_FILEXFER_ACMODTIME - // uint32 mtime present only if flag SSH_FILEXFER_ACMODTIME - // uint32 extended_count present only if flag SSH_FILEXFER_ATTR_EXTENDED - // string extended_type - // string extended_data - // ... more extended data (extended_type - extended_data pairs), - // so that number of pairs equals extended_count - - flags, fileStat := fileStatFromInfo(fi) - - b = marshalUint32(b, flags) - if flags&ssh_FILEXFER_ATTR_SIZE != 0 { - b = marshalUint64(b, fileStat.Size) - } - if flags&ssh_FILEXFER_ATTR_UIDGID != 0 { - b = marshalUint32(b, fileStat.UID) - b = marshalUint32(b, fileStat.GID) - } - if flags&ssh_FILEXFER_ATTR_PERMISSIONS != 0 { - b = marshalUint32(b, fileStat.Mode) - } - if flags&ssh_FILEXFER_ATTR_ACMODTIME != 0 { - b = marshalUint32(b, fileStat.Atime) - b = marshalUint32(b, fileStat.Mtime) - } - - return b -} - -// toFileMode converts sftp filemode bits to the os.FileMode specification -func toFileMode(mode uint32) os.FileMode { - var fm = os.FileMode(mode & 0777) - switch mode & syscall.S_IFMT { - case syscall.S_IFBLK: - fm |= os.ModeDevice - case syscall.S_IFCHR: - fm |= os.ModeDevice | os.ModeCharDevice - case syscall.S_IFDIR: - fm |= os.ModeDir - case syscall.S_IFIFO: - fm |= os.ModeNamedPipe - case syscall.S_IFLNK: - fm |= os.ModeSymlink - case syscall.S_IFREG: - // nothing to do - case syscall.S_IFSOCK: - fm |= os.ModeSocket - } - if mode&syscall.S_ISGID != 0 { - fm |= os.ModeSetgid - } - if mode&syscall.S_ISUID != 0 { - fm |= os.ModeSetuid - } - if mode&syscall.S_ISVTX != 0 { - fm |= os.ModeSticky - } - return fm -} - -// fromFileMode converts from the os.FileMode specification to sftp filemode bits -func fromFileMode(mode os.FileMode) uint32 { - ret := uint32(0) - - if mode&os.ModeDevice != 0 { - if mode&os.ModeCharDevice != 0 { - ret |= syscall.S_IFCHR - } else { - ret |= syscall.S_IFBLK - } - } - if mode&os.ModeDir != 0 { - ret |= syscall.S_IFDIR - } - if mode&os.ModeSymlink != 0 { - ret |= syscall.S_IFLNK - } - if mode&os.ModeNamedPipe != 0 { - ret |= syscall.S_IFIFO - } - if mode&os.ModeSetgid != 0 { - ret |= syscall.S_ISGID - } - if mode&os.ModeSetuid != 0 { - ret |= syscall.S_ISUID - } - if mode&os.ModeSticky != 0 { - ret |= syscall.S_ISVTX - } - if mode&os.ModeSocket != 0 { - ret |= syscall.S_IFSOCK - } - - if mode&os.ModeType == 0 { - ret |= syscall.S_IFREG - } - ret |= uint32(mode & os.ModePerm) - - return ret -} diff --git a/vendor/github.com/pkg/sftp/attrs_stubs.go b/vendor/github.com/pkg/sftp/attrs_stubs.go deleted file mode 100644 index 81cf3eac2bc..00000000000 --- a/vendor/github.com/pkg/sftp/attrs_stubs.go +++ /dev/null @@ -1,11 +0,0 @@ -// +build !cgo,!plan9 windows android - -package sftp - -import ( - "os" -) - -func fileStatFromInfoOs(fi os.FileInfo, flags *uint32, fileStat *FileStat) { - // todo -} diff --git a/vendor/github.com/pkg/sftp/attrs_unix.go b/vendor/github.com/pkg/sftp/attrs_unix.go deleted file mode 100644 index 95e7922e92d..00000000000 --- a/vendor/github.com/pkg/sftp/attrs_unix.go +++ /dev/null @@ -1,17 +0,0 @@ -// +build darwin dragonfly freebsd !android,linux netbsd openbsd solaris aix -// +build cgo - -package sftp - -import ( - "os" - "syscall" -) - -func fileStatFromInfoOs(fi os.FileInfo, flags *uint32, fileStat *FileStat) { - if statt, ok := fi.Sys().(*syscall.Stat_t); ok { - *flags |= ssh_FILEXFER_ATTR_UIDGID - fileStat.UID = statt.Uid - fileStat.GID = statt.Gid - } -} diff --git a/vendor/github.com/pkg/sftp/client.go b/vendor/github.com/pkg/sftp/client.go deleted file mode 100644 index 4f84fb5c57d..00000000000 --- a/vendor/github.com/pkg/sftp/client.go +++ /dev/null @@ -1,1282 +0,0 @@ -package sftp - -import ( - "bytes" - "encoding/binary" - "io" - "os" - "path" - "sync/atomic" - "syscall" - "time" - - "github.com/kr/fs" - "github.com/pkg/errors" - "golang.org/x/crypto/ssh" -) - -// InternalInconsistency indicates the packets sent and the data queued to be -// written to the file don't match up. It is an unusual error and usually is -// caused by bad behavior server side or connection issues. The error is -// limited in scope to the call where it happened, the client object is still -// OK to use as long as the connection is still open. -var InternalInconsistency = errors.New("internal inconsistency") - -// A ClientOption is a function which applies configuration to a Client. -type ClientOption func(*Client) error - -// MaxPacketChecked sets the maximum size of the payload, measured in bytes. -// This option only accepts sizes servers should support, ie. <= 32768 bytes. -// -// If you get the error "failed to send packet header: EOF" when copying a -// large file, try lowering this number. -// -// The default packet size is 32768 bytes. -func MaxPacketChecked(size int) ClientOption { - return func(c *Client) error { - if size < 1 { - return errors.Errorf("size must be greater or equal to 1") - } - if size > 32768 { - return errors.Errorf("sizes larger than 32KB might not work with all servers") - } - c.maxPacket = size - return nil - } -} - -// MaxPacketUnchecked sets the maximum size of the payload, measured in bytes. -// It accepts sizes larger than the 32768 bytes all servers should support. -// Only use a setting higher than 32768 if your application always connects to -// the same server or after sufficiently broad testing. -// -// If you get the error "failed to send packet header: EOF" when copying a -// large file, try lowering this number. -// -// The default packet size is 32768 bytes. -func MaxPacketUnchecked(size int) ClientOption { - return func(c *Client) error { - if size < 1 { - return errors.Errorf("size must be greater or equal to 1") - } - c.maxPacket = size - return nil - } -} - -// MaxPacket sets the maximum size of the payload, measured in bytes. -// This option only accepts sizes servers should support, ie. <= 32768 bytes. -// This is a synonym for MaxPacketChecked that provides backward compatibility. -// -// If you get the error "failed to send packet header: EOF" when copying a -// large file, try lowering this number. -// -// The default packet size is 32768 bytes. -func MaxPacket(size int) ClientOption { - return MaxPacketChecked(size) -} - -// MaxConcurrentRequestsPerFile sets the maximum concurrent requests allowed for a single file. -// -// The default maximum concurrent requests is 64. -func MaxConcurrentRequestsPerFile(n int) ClientOption { - return func(c *Client) error { - if n < 1 { - return errors.Errorf("n must be greater or equal to 1") - } - c.maxConcurrentRequests = n - return nil - } -} - -// NewClient creates a new SFTP client on conn, using zero or more option -// functions. -func NewClient(conn *ssh.Client, opts ...ClientOption) (*Client, error) { - s, err := conn.NewSession() - if err != nil { - return nil, err - } - if err := s.RequestSubsystem("sftp"); err != nil { - return nil, err - } - pw, err := s.StdinPipe() - if err != nil { - return nil, err - } - pr, err := s.StdoutPipe() - if err != nil { - return nil, err - } - - return NewClientPipe(pr, pw, opts...) -} - -// NewClientPipe creates a new SFTP client given a Reader and a WriteCloser. -// This can be used for connecting to an SFTP server over TCP/TLS or by using -// the system's ssh client program (e.g. via exec.Command). -func NewClientPipe(rd io.Reader, wr io.WriteCloser, opts ...ClientOption) (*Client, error) { - sftp := &Client{ - clientConn: clientConn{ - conn: conn{ - Reader: rd, - WriteCloser: wr, - }, - inflight: make(map[uint32]chan<- result), - }, - maxPacket: 1 << 15, - maxConcurrentRequests: 64, - } - if err := sftp.applyOptions(opts...); err != nil { - wr.Close() - return nil, err - } - if err := sftp.sendInit(); err != nil { - wr.Close() - return nil, err - } - if err := sftp.recvVersion(); err != nil { - wr.Close() - return nil, err - } - sftp.clientConn.wg.Add(1) - go sftp.loop() - return sftp, nil -} - -// Client represents an SFTP session on a *ssh.ClientConn SSH connection. -// Multiple Clients can be active on a single SSH connection, and a Client -// may be called concurrently from multiple Goroutines. -// -// Client implements the github.com/kr/fs.FileSystem interface. -type Client struct { - clientConn - - maxPacket int // max packet size read or written. - nextid uint32 - maxConcurrentRequests int -} - -// Create creates the named file mode 0666 (before umask), truncating it if it -// already exists. If successful, methods on the returned File can be used for -// I/O; the associated file descriptor has mode O_RDWR. If you need more -// control over the flags/mode used to open the file see client.OpenFile. -func (c *Client) Create(path string) (*File, error) { - return c.open(path, flags(os.O_RDWR|os.O_CREATE|os.O_TRUNC)) -} - -const sftpProtocolVersion = 3 // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02 - -func (c *Client) sendInit() error { - return c.clientConn.conn.sendPacket(sshFxInitPacket{ - Version: sftpProtocolVersion, // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02 - }) -} - -// returns the next value of c.nextid -func (c *Client) nextID() uint32 { - return atomic.AddUint32(&c.nextid, 1) -} - -func (c *Client) recvVersion() error { - typ, data, err := c.recvPacket() - if err != nil { - return err - } - if typ != ssh_FXP_VERSION { - return &unexpectedPacketErr{ssh_FXP_VERSION, typ} - } - - version, _ := unmarshalUint32(data) - if version != sftpProtocolVersion { - return &unexpectedVersionErr{sftpProtocolVersion, version} - } - - return nil -} - -// Walk returns a new Walker rooted at root. -func (c *Client) Walk(root string) *fs.Walker { - return fs.WalkFS(root, c) -} - -// ReadDir reads the directory named by dirname and returns a list of -// directory entries. -func (c *Client) ReadDir(p string) ([]os.FileInfo, error) { - handle, err := c.opendir(p) - if err != nil { - return nil, err - } - defer c.close(handle) // this has to defer earlier than the lock below - var attrs []os.FileInfo - var done = false - for !done { - id := c.nextID() - typ, data, err1 := c.sendPacket(sshFxpReaddirPacket{ - ID: id, - Handle: handle, - }) - if err1 != nil { - err = err1 - done = true - break - } - switch typ { - case ssh_FXP_NAME: - sid, data := unmarshalUint32(data) - if sid != id { - return nil, &unexpectedIDErr{id, sid} - } - count, data := unmarshalUint32(data) - for i := uint32(0); i < count; i++ { - var filename string - filename, data = unmarshalString(data) - _, data = unmarshalString(data) // discard longname - var attr *FileStat - attr, data = unmarshalAttrs(data) - if filename == "." || filename == ".." { - continue - } - attrs = append(attrs, fileInfoFromStat(attr, path.Base(filename))) - } - case ssh_FXP_STATUS: - // TODO(dfc) scope warning! - err = normaliseError(unmarshalStatus(id, data)) - done = true - default: - return nil, unimplementedPacketErr(typ) - } - } - if err == io.EOF { - err = nil - } - return attrs, err -} - -func (c *Client) opendir(path string) (string, error) { - id := c.nextID() - typ, data, err := c.sendPacket(sshFxpOpendirPacket{ - ID: id, - Path: path, - }) - if err != nil { - return "", err - } - switch typ { - case ssh_FXP_HANDLE: - sid, data := unmarshalUint32(data) - if sid != id { - return "", &unexpectedIDErr{id, sid} - } - handle, _ := unmarshalString(data) - return handle, nil - case ssh_FXP_STATUS: - return "", normaliseError(unmarshalStatus(id, data)) - default: - return "", unimplementedPacketErr(typ) - } -} - -// Stat returns a FileInfo structure describing the file specified by path 'p'. -// If 'p' is a symbolic link, the returned FileInfo structure describes the referent file. -func (c *Client) Stat(p string) (os.FileInfo, error) { - id := c.nextID() - typ, data, err := c.sendPacket(sshFxpStatPacket{ - ID: id, - Path: p, - }) - if err != nil { - return nil, err - } - switch typ { - case ssh_FXP_ATTRS: - sid, data := unmarshalUint32(data) - if sid != id { - return nil, &unexpectedIDErr{id, sid} - } - attr, _ := unmarshalAttrs(data) - return fileInfoFromStat(attr, path.Base(p)), nil - case ssh_FXP_STATUS: - return nil, normaliseError(unmarshalStatus(id, data)) - default: - return nil, unimplementedPacketErr(typ) - } -} - -// Lstat returns a FileInfo structure describing the file specified by path 'p'. -// If 'p' is a symbolic link, the returned FileInfo structure describes the symbolic link. -func (c *Client) Lstat(p string) (os.FileInfo, error) { - id := c.nextID() - typ, data, err := c.sendPacket(sshFxpLstatPacket{ - ID: id, - Path: p, - }) - if err != nil { - return nil, err - } - switch typ { - case ssh_FXP_ATTRS: - sid, data := unmarshalUint32(data) - if sid != id { - return nil, &unexpectedIDErr{id, sid} - } - attr, _ := unmarshalAttrs(data) - return fileInfoFromStat(attr, path.Base(p)), nil - case ssh_FXP_STATUS: - return nil, normaliseError(unmarshalStatus(id, data)) - default: - return nil, unimplementedPacketErr(typ) - } -} - -// ReadLink reads the target of a symbolic link. -func (c *Client) ReadLink(p string) (string, error) { - id := c.nextID() - typ, data, err := c.sendPacket(sshFxpReadlinkPacket{ - ID: id, - Path: p, - }) - if err != nil { - return "", err - } - switch typ { - case ssh_FXP_NAME: - sid, data := unmarshalUint32(data) - if sid != id { - return "", &unexpectedIDErr{id, sid} - } - count, data := unmarshalUint32(data) - if count != 1 { - return "", unexpectedCount(1, count) - } - filename, _ := unmarshalString(data) // ignore dummy attributes - return filename, nil - case ssh_FXP_STATUS: - return "", normaliseError(unmarshalStatus(id, data)) - default: - return "", unimplementedPacketErr(typ) - } -} - -// Symlink creates a symbolic link at 'newname', pointing at target 'oldname' -func (c *Client) Symlink(oldname, newname string) error { - id := c.nextID() - typ, data, err := c.sendPacket(sshFxpSymlinkPacket{ - ID: id, - Linkpath: newname, - Targetpath: oldname, - }) - if err != nil { - return err - } - switch typ { - case ssh_FXP_STATUS: - return normaliseError(unmarshalStatus(id, data)) - default: - return unimplementedPacketErr(typ) - } -} - -// setstat is a convience wrapper to allow for changing of various parts of the file descriptor. -func (c *Client) setstat(path string, flags uint32, attrs interface{}) error { - id := c.nextID() - typ, data, err := c.sendPacket(sshFxpSetstatPacket{ - ID: id, - Path: path, - Flags: flags, - Attrs: attrs, - }) - if err != nil { - return err - } - switch typ { - case ssh_FXP_STATUS: - return normaliseError(unmarshalStatus(id, data)) - default: - return unimplementedPacketErr(typ) - } -} - -// Chtimes changes the access and modification times of the named file. -func (c *Client) Chtimes(path string, atime time.Time, mtime time.Time) error { - type times struct { - Atime uint32 - Mtime uint32 - } - attrs := times{uint32(atime.Unix()), uint32(mtime.Unix())} - return c.setstat(path, ssh_FILEXFER_ATTR_ACMODTIME, attrs) -} - -// Chown changes the user and group owners of the named file. -func (c *Client) Chown(path string, uid, gid int) error { - type owner struct { - UID uint32 - GID uint32 - } - attrs := owner{uint32(uid), uint32(gid)} - return c.setstat(path, ssh_FILEXFER_ATTR_UIDGID, attrs) -} - -// Chmod changes the permissions of the named file. -func (c *Client) Chmod(path string, mode os.FileMode) error { - return c.setstat(path, ssh_FILEXFER_ATTR_PERMISSIONS, uint32(mode)) -} - -// Truncate sets the size of the named file. Although it may be safely assumed -// that if the size is less than its current size it will be truncated to fit, -// the SFTP protocol does not specify what behavior the server should do when setting -// size greater than the current size. -func (c *Client) Truncate(path string, size int64) error { - return c.setstat(path, ssh_FILEXFER_ATTR_SIZE, uint64(size)) -} - -// Open opens the named file for reading. If successful, methods on the -// returned file can be used for reading; the associated file descriptor -// has mode O_RDONLY. -func (c *Client) Open(path string) (*File, error) { - return c.open(path, flags(os.O_RDONLY)) -} - -// OpenFile is the generalized open call; most users will use Open or -// Create instead. It opens the named file with specified flag (O_RDONLY -// etc.). If successful, methods on the returned File can be used for I/O. -func (c *Client) OpenFile(path string, f int) (*File, error) { - return c.open(path, flags(f)) -} - -func (c *Client) open(path string, pflags uint32) (*File, error) { - id := c.nextID() - typ, data, err := c.sendPacket(sshFxpOpenPacket{ - ID: id, - Path: path, - Pflags: pflags, - }) - if err != nil { - return nil, err - } - switch typ { - case ssh_FXP_HANDLE: - sid, data := unmarshalUint32(data) - if sid != id { - return nil, &unexpectedIDErr{id, sid} - } - handle, _ := unmarshalString(data) - return &File{c: c, path: path, handle: handle}, nil - case ssh_FXP_STATUS: - return nil, normaliseError(unmarshalStatus(id, data)) - default: - return nil, unimplementedPacketErr(typ) - } -} - -// close closes a handle handle previously returned in the response -// to SSH_FXP_OPEN or SSH_FXP_OPENDIR. The handle becomes invalid -// immediately after this request has been sent. -func (c *Client) close(handle string) error { - id := c.nextID() - typ, data, err := c.sendPacket(sshFxpClosePacket{ - ID: id, - Handle: handle, - }) - if err != nil { - return err - } - switch typ { - case ssh_FXP_STATUS: - return normaliseError(unmarshalStatus(id, data)) - default: - return unimplementedPacketErr(typ) - } -} - -func (c *Client) fstat(handle string) (*FileStat, error) { - id := c.nextID() - typ, data, err := c.sendPacket(sshFxpFstatPacket{ - ID: id, - Handle: handle, - }) - if err != nil { - return nil, err - } - switch typ { - case ssh_FXP_ATTRS: - sid, data := unmarshalUint32(data) - if sid != id { - return nil, &unexpectedIDErr{id, sid} - } - attr, _ := unmarshalAttrs(data) - return attr, nil - case ssh_FXP_STATUS: - return nil, normaliseError(unmarshalStatus(id, data)) - default: - return nil, unimplementedPacketErr(typ) - } -} - -// StatVFS retrieves VFS statistics from a remote host. -// -// It implements the statvfs@openssh.com SSH_FXP_EXTENDED feature -// from http://www.opensource.apple.com/source/OpenSSH/OpenSSH-175/openssh/PROTOCOL?txt. -func (c *Client) StatVFS(path string) (*StatVFS, error) { - // send the StatVFS packet to the server - id := c.nextID() - typ, data, err := c.sendPacket(sshFxpStatvfsPacket{ - ID: id, - Path: path, - }) - if err != nil { - return nil, err - } - - switch typ { - // server responded with valid data - case ssh_FXP_EXTENDED_REPLY: - var response StatVFS - err = binary.Read(bytes.NewReader(data), binary.BigEndian, &response) - if err != nil { - return nil, errors.New("can not parse reply") - } - - return &response, nil - - // the resquest failed - case ssh_FXP_STATUS: - return nil, errors.New(fxp(ssh_FXP_STATUS).String()) - - default: - return nil, unimplementedPacketErr(typ) - } -} - -// Join joins any number of path elements into a single path, adding a -// separating slash if necessary. The result is Cleaned; in particular, all -// empty strings are ignored. -func (c *Client) Join(elem ...string) string { return path.Join(elem...) } - -// Remove removes the specified file or directory. An error will be returned if no -// file or directory with the specified path exists, or if the specified directory -// is not empty. -func (c *Client) Remove(path string) error { - err := c.removeFile(path) - if err, ok := err.(*StatusError); ok { - switch err.Code { - // some servers, *cough* osx *cough*, return EPERM, not ENODIR. - // serv-u returns ssh_FX_FILE_IS_A_DIRECTORY - case ssh_FX_PERMISSION_DENIED, ssh_FX_FAILURE, ssh_FX_FILE_IS_A_DIRECTORY: - return c.RemoveDirectory(path) - } - } - return err -} - -func (c *Client) removeFile(path string) error { - id := c.nextID() - typ, data, err := c.sendPacket(sshFxpRemovePacket{ - ID: id, - Filename: path, - }) - if err != nil { - return err - } - switch typ { - case ssh_FXP_STATUS: - return normaliseError(unmarshalStatus(id, data)) - default: - return unimplementedPacketErr(typ) - } -} - -// RemoveDirectory removes a directory path. -func (c *Client) RemoveDirectory(path string) error { - id := c.nextID() - typ, data, err := c.sendPacket(sshFxpRmdirPacket{ - ID: id, - Path: path, - }) - if err != nil { - return err - } - switch typ { - case ssh_FXP_STATUS: - return normaliseError(unmarshalStatus(id, data)) - default: - return unimplementedPacketErr(typ) - } -} - -// Rename renames a file. -func (c *Client) Rename(oldname, newname string) error { - id := c.nextID() - typ, data, err := c.sendPacket(sshFxpRenamePacket{ - ID: id, - Oldpath: oldname, - Newpath: newname, - }) - if err != nil { - return err - } - switch typ { - case ssh_FXP_STATUS: - return normaliseError(unmarshalStatus(id, data)) - default: - return unimplementedPacketErr(typ) - } -} - -// PosixRename renames a file using the posix-rename@openssh.com extension -// which will replace newname if it already exists. -func (c *Client) PosixRename(oldname, newname string) error { - id := c.nextID() - typ, data, err := c.sendPacket(sshFxpPosixRenamePacket{ - ID: id, - Oldpath: oldname, - Newpath: newname, - }) - if err != nil { - return err - } - switch typ { - case ssh_FXP_STATUS: - return normaliseError(unmarshalStatus(id, data)) - default: - return unimplementedPacketErr(typ) - } -} - -func (c *Client) realpath(path string) (string, error) { - id := c.nextID() - typ, data, err := c.sendPacket(sshFxpRealpathPacket{ - ID: id, - Path: path, - }) - if err != nil { - return "", err - } - switch typ { - case ssh_FXP_NAME: - sid, data := unmarshalUint32(data) - if sid != id { - return "", &unexpectedIDErr{id, sid} - } - count, data := unmarshalUint32(data) - if count != 1 { - return "", unexpectedCount(1, count) - } - filename, _ := unmarshalString(data) // ignore attributes - return filename, nil - case ssh_FXP_STATUS: - return "", normaliseError(unmarshalStatus(id, data)) - default: - return "", unimplementedPacketErr(typ) - } -} - -// Getwd returns the current working directory of the server. Operations -// involving relative paths will be based at this location. -func (c *Client) Getwd() (string, error) { - return c.realpath(".") -} - -// Mkdir creates the specified directory. An error will be returned if a file or -// directory with the specified path already exists, or if the directory's -// parent folder does not exist (the method cannot create complete paths). -func (c *Client) Mkdir(path string) error { - id := c.nextID() - typ, data, err := c.sendPacket(sshFxpMkdirPacket{ - ID: id, - Path: path, - }) - if err != nil { - return err - } - switch typ { - case ssh_FXP_STATUS: - return normaliseError(unmarshalStatus(id, data)) - default: - return unimplementedPacketErr(typ) - } -} - -// MkdirAll creates a directory named path, along with any necessary parents, -// and returns nil, or else returns an error. -// If path is already a directory, MkdirAll does nothing and returns nil. -// If path contains a regular file, an error is returned -func (c *Client) MkdirAll(path string) error { - // Most of this code mimics https://golang.org/src/os/path.go?s=514:561#L13 - // Fast path: if we can tell whether path is a directory or file, stop with success or error. - dir, err := c.Stat(path) - if err == nil { - if dir.IsDir() { - return nil - } - return &os.PathError{Op: "mkdir", Path: path, Err: syscall.ENOTDIR} - } - - // Slow path: make sure parent exists and then call Mkdir for path. - i := len(path) - for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator. - i-- - } - - j := i - for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element. - j-- - } - - if j > 1 { - // Create parent - err = c.MkdirAll(path[0 : j-1]) - if err != nil { - return err - } - } - - // Parent now exists; invoke Mkdir and use its result. - err = c.Mkdir(path) - if err != nil { - // Handle arguments like "foo/." by - // double-checking that directory doesn't exist. - dir, err1 := c.Lstat(path) - if err1 == nil && dir.IsDir() { - return nil - } - return err - } - return nil -} - -// applyOptions applies options functions to the Client. -// If an error is encountered, option processing ceases. -func (c *Client) applyOptions(opts ...ClientOption) error { - for _, f := range opts { - if err := f(c); err != nil { - return err - } - } - return nil -} - -// File represents a remote file. -type File struct { - c *Client - path string - handle string - offset uint64 // current offset within remote file -} - -// Close closes the File, rendering it unusable for I/O. It returns an -// error, if any. -func (f *File) Close() error { - return f.c.close(f.handle) -} - -// Name returns the name of the file as presented to Open or Create. -func (f *File) Name() string { - return f.path -} - -// Read reads up to len(b) bytes from the File. It returns the number of bytes -// read and an error, if any. Read follows io.Reader semantics, so when Read -// encounters an error or EOF condition after successfully reading n > 0 bytes, -// it returns the number of bytes read. -// -// To maximise throughput for transferring the entire file (especially -// over high latency links) it is recommended to use WriteTo rather -// than calling Read multiple times. io.Copy will do this -// automatically. -func (f *File) Read(b []byte) (int, error) { - // Split the read into multiple maxPacket sized concurrent reads - // bounded by maxConcurrentRequests. This allows reads with a suitably - // large buffer to transfer data at a much faster rate due to - // overlapping round trip times. - inFlight := 0 - desiredInFlight := 1 - offset := f.offset - // maxConcurrentRequests buffer to deal with broadcastErr() floods - // also must have a buffer of max value of (desiredInFlight - inFlight) - ch := make(chan result, f.c.maxConcurrentRequests+1) - type inflightRead struct { - b []byte - offset uint64 - } - reqs := map[uint32]inflightRead{} - type offsetErr struct { - offset uint64 - err error - } - var firstErr offsetErr - - sendReq := func(b []byte, offset uint64) { - reqID := f.c.nextID() - f.c.dispatchRequest(ch, sshFxpReadPacket{ - ID: reqID, - Handle: f.handle, - Offset: offset, - Len: uint32(len(b)), - }) - inFlight++ - reqs[reqID] = inflightRead{b: b, offset: offset} - } - - var read int - for len(b) > 0 || inFlight > 0 { - for inFlight < desiredInFlight && len(b) > 0 && firstErr.err == nil { - l := min(len(b), f.c.maxPacket) - rb := b[:l] - sendReq(rb, offset) - offset += uint64(l) - b = b[l:] - } - - if inFlight == 0 { - break - } - res := <-ch - inFlight-- - if res.err != nil { - firstErr = offsetErr{offset: 0, err: res.err} - continue - } - reqID, data := unmarshalUint32(res.data) - req, ok := reqs[reqID] - if !ok { - firstErr = offsetErr{offset: 0, err: errors.Errorf("sid: %v not found", reqID)} - continue - } - delete(reqs, reqID) - switch res.typ { - case ssh_FXP_STATUS: - if firstErr.err == nil || req.offset < firstErr.offset { - firstErr = offsetErr{ - offset: req.offset, - err: normaliseError(unmarshalStatus(reqID, res.data)), - } - } - case ssh_FXP_DATA: - l, data := unmarshalUint32(data) - n := copy(req.b, data[:l]) - read += n - if n < len(req.b) { - sendReq(req.b[l:], req.offset+uint64(l)) - } - if desiredInFlight < f.c.maxConcurrentRequests { - desiredInFlight++ - } - default: - firstErr = offsetErr{offset: 0, err: unimplementedPacketErr(res.typ)} - } - } - // If the error is anything other than EOF, then there - // may be gaps in the data copied to the buffer so it's - // best to return 0 so the caller can't make any - // incorrect assumptions about the state of the buffer. - if firstErr.err != nil && firstErr.err != io.EOF { - read = 0 - } - f.offset += uint64(read) - return read, firstErr.err -} - -// WriteTo writes the file to w. The return value is the number of bytes -// written. Any error encountered during the write is also returned. -// -// This method is preferred over calling Read multiple times to -// maximise throughput for transferring the entire file (especially -// over high latency links). -func (f *File) WriteTo(w io.Writer) (int64, error) { - fi, err := f.Stat() - if err != nil { - return 0, err - } - inFlight := 0 - desiredInFlight := 1 - offset := f.offset - writeOffset := offset - fileSize := uint64(fi.Size()) - // see comment on same line in Read() above - ch := make(chan result, f.c.maxConcurrentRequests+1) - type inflightRead struct { - b []byte - offset uint64 - } - reqs := map[uint32]inflightRead{} - pendingWrites := map[uint64][]byte{} - type offsetErr struct { - offset uint64 - err error - } - var firstErr offsetErr - - sendReq := func(b []byte, offset uint64) { - reqID := f.c.nextID() - f.c.dispatchRequest(ch, sshFxpReadPacket{ - ID: reqID, - Handle: f.handle, - Offset: offset, - Len: uint32(len(b)), - }) - inFlight++ - reqs[reqID] = inflightRead{b: b, offset: offset} - } - - var copied int64 - for firstErr.err == nil || inFlight > 0 { - if firstErr.err == nil { - for inFlight+len(pendingWrites) < desiredInFlight { - b := make([]byte, f.c.maxPacket) - sendReq(b, offset) - offset += uint64(f.c.maxPacket) - if offset > fileSize { - desiredInFlight = 1 - } - } - } - - if inFlight == 0 { - if firstErr.err == nil && len(pendingWrites) > 0 { - return copied, InternalInconsistency - } - break - } - res := <-ch - inFlight-- - if res.err != nil { - firstErr = offsetErr{offset: 0, err: res.err} - continue - } - reqID, data := unmarshalUint32(res.data) - req, ok := reqs[reqID] - if !ok { - firstErr = offsetErr{offset: 0, err: errors.Errorf("sid: %v not found", reqID)} - continue - } - delete(reqs, reqID) - switch res.typ { - case ssh_FXP_STATUS: - if firstErr.err == nil || req.offset < firstErr.offset { - firstErr = offsetErr{offset: req.offset, err: normaliseError(unmarshalStatus(reqID, res.data))} - } - case ssh_FXP_DATA: - l, data := unmarshalUint32(data) - if req.offset == writeOffset { - nbytes, err := w.Write(data) - copied += int64(nbytes) - if err != nil { - // We will never receive another DATA with offset==writeOffset, so - // the loop will drain inFlight and then exit. - firstErr = offsetErr{offset: req.offset + uint64(nbytes), err: err} - break - } - if nbytes < int(l) { - firstErr = offsetErr{offset: req.offset + uint64(nbytes), err: io.ErrShortWrite} - break - } - switch { - case offset > fileSize: - desiredInFlight = 1 - case desiredInFlight < f.c.maxConcurrentRequests: - desiredInFlight++ - } - writeOffset += uint64(nbytes) - for { - pendingData, ok := pendingWrites[writeOffset] - if !ok { - break - } - // Give go a chance to free the memory. - delete(pendingWrites, writeOffset) - nbytes, err := w.Write(pendingData) - // Do not move writeOffset on error so subsequent iterations won't trigger - // any writes. - if err != nil { - firstErr = offsetErr{offset: writeOffset + uint64(nbytes), err: err} - break - } - if nbytes < len(pendingData) { - firstErr = offsetErr{offset: writeOffset + uint64(nbytes), err: io.ErrShortWrite} - break - } - writeOffset += uint64(nbytes) - } - } else { - // Don't write the data yet because - // this response came in out of order - // and we need to wait for responses - // for earlier segments of the file. - pendingWrites[req.offset] = data - } - default: - firstErr = offsetErr{offset: 0, err: unimplementedPacketErr(res.typ)} - } - } - if firstErr.err != io.EOF { - return copied, firstErr.err - } - return copied, nil -} - -// Stat returns the FileInfo structure describing file. If there is an -// error. -func (f *File) Stat() (os.FileInfo, error) { - fs, err := f.c.fstat(f.handle) - if err != nil { - return nil, err - } - return fileInfoFromStat(fs, path.Base(f.path)), nil -} - -// Write writes len(b) bytes to the File. It returns the number of bytes -// written and an error, if any. Write returns a non-nil error when n != -// len(b). -// -// To maximise throughput for transferring the entire file (especially -// over high latency links) it is recommended to use ReadFrom rather -// than calling Write multiple times. io.Copy will do this -// automatically. -func (f *File) Write(b []byte) (int, error) { - // Split the write into multiple maxPacket sized concurrent writes - // bounded by maxConcurrentRequests. This allows writes with a suitably - // large buffer to transfer data at a much faster rate due to - // overlapping round trip times. - inFlight := 0 - desiredInFlight := 1 - offset := f.offset - // see comment on same line in Read() above - ch := make(chan result, f.c.maxConcurrentRequests+1) - var firstErr error - written := len(b) - for len(b) > 0 || inFlight > 0 { - for inFlight < desiredInFlight && len(b) > 0 && firstErr == nil { - l := min(len(b), f.c.maxPacket) - rb := b[:l] - f.c.dispatchRequest(ch, sshFxpWritePacket{ - ID: f.c.nextID(), - Handle: f.handle, - Offset: offset, - Length: uint32(len(rb)), - Data: rb, - }) - inFlight++ - offset += uint64(l) - b = b[l:] - } - - if inFlight == 0 { - break - } - res := <-ch - inFlight-- - if res.err != nil { - firstErr = res.err - continue - } - switch res.typ { - case ssh_FXP_STATUS: - id, _ := unmarshalUint32(res.data) - err := normaliseError(unmarshalStatus(id, res.data)) - if err != nil && firstErr == nil { - firstErr = err - break - } - if desiredInFlight < f.c.maxConcurrentRequests { - desiredInFlight++ - } - default: - firstErr = unimplementedPacketErr(res.typ) - } - } - // If error is non-nil, then there may be gaps in the data written to - // the file so it's best to return 0 so the caller can't make any - // incorrect assumptions about the state of the file. - if firstErr != nil { - written = 0 - } - f.offset += uint64(written) - return written, firstErr -} - -// ReadFrom reads data from r until EOF and writes it to the file. The return -// value is the number of bytes read. Any error except io.EOF encountered -// during the read is also returned. -// -// This method is preferred over calling Write multiple times to -// maximise throughput for transferring the entire file (especially -// over high latency links). -func (f *File) ReadFrom(r io.Reader) (int64, error) { - inFlight := 0 - desiredInFlight := 1 - offset := f.offset - // see comment on same line in Read() above - ch := make(chan result, f.c.maxConcurrentRequests+1) - var firstErr error - read := int64(0) - b := make([]byte, f.c.maxPacket) - for inFlight > 0 || firstErr == nil { - for inFlight < desiredInFlight && firstErr == nil { - n, err := r.Read(b) - if err != nil { - firstErr = err - } - f.c.dispatchRequest(ch, sshFxpWritePacket{ - ID: f.c.nextID(), - Handle: f.handle, - Offset: offset, - Length: uint32(n), - Data: b[:n], - }) - inFlight++ - offset += uint64(n) - read += int64(n) - } - - if inFlight == 0 { - break - } - res := <-ch - inFlight-- - if res.err != nil { - firstErr = res.err - continue - } - switch res.typ { - case ssh_FXP_STATUS: - id, _ := unmarshalUint32(res.data) - err := normaliseError(unmarshalStatus(id, res.data)) - if err != nil && firstErr == nil { - firstErr = err - break - } - if desiredInFlight < f.c.maxConcurrentRequests { - desiredInFlight++ - } - default: - firstErr = unimplementedPacketErr(res.typ) - } - } - if firstErr == io.EOF { - firstErr = nil - } - // If error is non-nil, then there may be gaps in the data written to - // the file so it's best to return 0 so the caller can't make any - // incorrect assumptions about the state of the file. - if firstErr != nil { - read = 0 - } - f.offset += uint64(read) - return read, firstErr -} - -// Seek implements io.Seeker by setting the client offset for the next Read or -// Write. It returns the next offset read. Seeking before or after the end of -// the file is undefined. Seeking relative to the end calls Stat. -func (f *File) Seek(offset int64, whence int) (int64, error) { - switch whence { - case io.SeekStart: - f.offset = uint64(offset) - case io.SeekCurrent: - f.offset = uint64(int64(f.offset) + offset) - case io.SeekEnd: - fi, err := f.Stat() - if err != nil { - return int64(f.offset), err - } - f.offset = uint64(fi.Size() + offset) - default: - return int64(f.offset), unimplementedSeekWhence(whence) - } - return int64(f.offset), nil -} - -// Chown changes the uid/gid of the current file. -func (f *File) Chown(uid, gid int) error { - return f.c.Chown(f.path, uid, gid) -} - -// Chmod changes the permissions of the current file. -func (f *File) Chmod(mode os.FileMode) error { - return f.c.Chmod(f.path, mode) -} - -// Truncate sets the size of the current file. Although it may be safely assumed -// that if the size is less than its current size it will be truncated to fit, -// the SFTP protocol does not specify what behavior the server should do when setting -// size greater than the current size. -func (f *File) Truncate(size int64) error { - return f.c.Truncate(f.path, size) -} - -func min(a, b int) int { - if a > b { - return b - } - return a -} - -// normaliseError normalises an error into a more standard form that can be -// checked against stdlib errors like io.EOF or os.ErrNotExist. -func normaliseError(err error) error { - switch err := err.(type) { - case *StatusError: - switch err.Code { - case ssh_FX_EOF: - return io.EOF - case ssh_FX_NO_SUCH_FILE: - return os.ErrNotExist - case ssh_FX_OK: - return nil - default: - return err - } - default: - return err - } -} - -func unmarshalStatus(id uint32, data []byte) error { - sid, data := unmarshalUint32(data) - if sid != id { - return &unexpectedIDErr{id, sid} - } - code, data := unmarshalUint32(data) - msg, data, _ := unmarshalStringSafe(data) - lang, _, _ := unmarshalStringSafe(data) - return &StatusError{ - Code: code, - msg: msg, - lang: lang, - } -} - -func marshalStatus(b []byte, err StatusError) []byte { - b = marshalUint32(b, err.Code) - b = marshalString(b, err.msg) - b = marshalString(b, err.lang) - return b -} - -// flags converts the flags passed to OpenFile into ssh flags. -// Unsupported flags are ignored. -func flags(f int) uint32 { - var out uint32 - switch f & os.O_WRONLY { - case os.O_WRONLY: - out |= ssh_FXF_WRITE - case os.O_RDONLY: - out |= ssh_FXF_READ - } - if f&os.O_RDWR == os.O_RDWR { - out |= ssh_FXF_READ | ssh_FXF_WRITE - } - if f&os.O_APPEND == os.O_APPEND { - out |= ssh_FXF_APPEND - } - if f&os.O_CREATE == os.O_CREATE { - out |= ssh_FXF_CREAT - } - if f&os.O_TRUNC == os.O_TRUNC { - out |= ssh_FXF_TRUNC - } - if f&os.O_EXCL == os.O_EXCL { - out |= ssh_FXF_EXCL - } - return out -} diff --git a/vendor/github.com/pkg/sftp/conn.go b/vendor/github.com/pkg/sftp/conn.go deleted file mode 100644 index f799715edc4..00000000000 --- a/vendor/github.com/pkg/sftp/conn.go +++ /dev/null @@ -1,133 +0,0 @@ -package sftp - -import ( - "encoding" - "io" - "sync" - - "github.com/pkg/errors" -) - -// conn implements a bidirectional channel on which client and server -// connections are multiplexed. -type conn struct { - io.Reader - io.WriteCloser - sync.Mutex // used to serialise writes to sendPacket - // sendPacketTest is needed to replicate packet issues in testing - sendPacketTest func(w io.Writer, m encoding.BinaryMarshaler) error -} - -func (c *conn) recvPacket() (uint8, []byte, error) { - return recvPacket(c) -} - -func (c *conn) sendPacket(m encoding.BinaryMarshaler) error { - c.Lock() - defer c.Unlock() - if c.sendPacketTest != nil { - return c.sendPacketTest(c, m) - } - return sendPacket(c, m) -} - -type clientConn struct { - conn - wg sync.WaitGroup - sync.Mutex // protects inflight - inflight map[uint32]chan<- result // outstanding requests -} - -// Close closes the SFTP session. -func (c *clientConn) Close() error { - defer c.wg.Wait() - return c.conn.Close() -} - -func (c *clientConn) loop() { - defer c.wg.Done() - err := c.recv() - if err != nil { - c.broadcastErr(err) - } -} - -// recv continuously reads from the server and forwards responses to the -// appropriate channel. -func (c *clientConn) recv() error { - defer func() { - c.conn.Lock() - c.conn.Close() - c.conn.Unlock() - }() - for { - typ, data, err := c.recvPacket() - if err != nil { - return err - } - sid, _ := unmarshalUint32(data) - c.Lock() - ch, ok := c.inflight[sid] - delete(c.inflight, sid) - c.Unlock() - if !ok { - // This is an unexpected occurrence. Send the error - // back to all listeners so that they terminate - // gracefully. - return errors.Errorf("sid: %v not fond", sid) - } - ch <- result{typ: typ, data: data} - } -} - -// result captures the result of receiving the a packet from the server -type result struct { - typ byte - data []byte - err error -} - -type idmarshaler interface { - id() uint32 - encoding.BinaryMarshaler -} - -func (c *clientConn) sendPacket(p idmarshaler) (byte, []byte, error) { - ch := make(chan result, 2) - c.dispatchRequest(ch, p) - s := <-ch - return s.typ, s.data, s.err -} - -func (c *clientConn) dispatchRequest(ch chan<- result, p idmarshaler) { - c.Lock() - c.inflight[p.id()] = ch - c.Unlock() - if err := c.conn.sendPacket(p); err != nil { - c.Lock() - delete(c.inflight, p.id()) - c.Unlock() - ch <- result{err: err} - } -} - -// broadcastErr sends an error to all goroutines waiting for a response. -func (c *clientConn) broadcastErr(err error) { - c.Lock() - listeners := make([]chan<- result, 0, len(c.inflight)) - for _, ch := range c.inflight { - listeners = append(listeners, ch) - } - c.Unlock() - for _, ch := range listeners { - ch <- result{err: err} - } -} - -type serverConn struct { - conn -} - -func (s *serverConn) sendError(p ider, err error) error { - return s.sendPacket(statusFromError(p, err)) -} diff --git a/vendor/github.com/pkg/sftp/debug.go b/vendor/github.com/pkg/sftp/debug.go deleted file mode 100644 index 3e264abe303..00000000000 --- a/vendor/github.com/pkg/sftp/debug.go +++ /dev/null @@ -1,9 +0,0 @@ -// +build debug - -package sftp - -import "log" - -func debug(fmt string, args ...interface{}) { - log.Printf(fmt, args...) -} diff --git a/vendor/github.com/pkg/sftp/match.go b/vendor/github.com/pkg/sftp/match.go deleted file mode 100644 index e2f2ba409ef..00000000000 --- a/vendor/github.com/pkg/sftp/match.go +++ /dev/null @@ -1,295 +0,0 @@ -package sftp - -import ( - "path" - "strings" - "unicode/utf8" -) - -// ErrBadPattern indicates a globbing pattern was malformed. -var ErrBadPattern = path.ErrBadPattern - -// Unix separator -const separator = "/" - -// Match reports whether name matches the shell file name pattern. -// The pattern syntax is: -// -// pattern: -// { term } -// term: -// '*' matches any sequence of non-Separator characters -// '?' matches any single non-Separator character -// '[' [ '^' ] { character-range } ']' -// character class (must be non-empty) -// c matches character c (c != '*', '?', '\\', '[') -// '\\' c matches character c -// -// character-range: -// c matches character c (c != '\\', '-', ']') -// '\\' c matches character c -// lo '-' hi matches character c for lo <= c <= hi -// -// Match requires pattern to match all of name, not just a substring. -// The only possible returned error is ErrBadPattern, when pattern -// is malformed. -// -// -func Match(pattern, name string) (matched bool, err error) { - return path.Match(pattern, name) -} - -// detect if byte(char) is path separator -func isPathSeparator(c byte) bool { - return string(c) == "/" -} - -// scanChunk gets the next segment of pattern, which is a non-star string -// possibly preceded by a star. -func scanChunk(pattern string) (star bool, chunk, rest string) { - for len(pattern) > 0 && pattern[0] == '*' { - pattern = pattern[1:] - star = true - } - inrange := false - var i int -Scan: - for i = 0; i < len(pattern); i++ { - switch pattern[i] { - case '\\': - - // error check handled in matchChunk: bad pattern. - if i+1 < len(pattern) { - i++ - } - case '[': - inrange = true - case ']': - inrange = false - case '*': - if !inrange { - break Scan - } - } - } - return star, pattern[0:i], pattern[i:] -} - -// matchChunk checks whether chunk matches the beginning of s. -// If so, it returns the remainder of s (after the match). -// Chunk is all single-character operators: literals, char classes, and ?. -func matchChunk(chunk, s string) (rest string, ok bool, err error) { - for len(chunk) > 0 { - if len(s) == 0 { - return - } - switch chunk[0] { - case '[': - // character class - r, n := utf8.DecodeRuneInString(s) - s = s[n:] - chunk = chunk[1:] - // We can't end right after '[', we're expecting at least - // a closing bracket and possibly a caret. - if len(chunk) == 0 { - err = ErrBadPattern - return - } - // possibly negated - negated := chunk[0] == '^' - if negated { - chunk = chunk[1:] - } - // parse all ranges - match := false - nrange := 0 - for { - if len(chunk) > 0 && chunk[0] == ']' && nrange > 0 { - chunk = chunk[1:] - break - } - var lo, hi rune - if lo, chunk, err = getEsc(chunk); err != nil { - return - } - hi = lo - if chunk[0] == '-' { - if hi, chunk, err = getEsc(chunk[1:]); err != nil { - return - } - } - if lo <= r && r <= hi { - match = true - } - nrange++ - } - if match == negated { - return - } - - case '?': - if isPathSeparator(s[0]) { - return - } - _, n := utf8.DecodeRuneInString(s) - s = s[n:] - chunk = chunk[1:] - - case '\\': - chunk = chunk[1:] - if len(chunk) == 0 { - err = ErrBadPattern - return - } - fallthrough - - default: - if chunk[0] != s[0] { - return - } - s = s[1:] - chunk = chunk[1:] - } - } - return s, true, nil -} - -// getEsc gets a possibly-escaped character from chunk, for a character class. -func getEsc(chunk string) (r rune, nchunk string, err error) { - if len(chunk) == 0 || chunk[0] == '-' || chunk[0] == ']' { - err = ErrBadPattern - return - } - if chunk[0] == '\\' { - chunk = chunk[1:] - if len(chunk) == 0 { - err = ErrBadPattern - return - } - } - r, n := utf8.DecodeRuneInString(chunk) - if r == utf8.RuneError && n == 1 { - err = ErrBadPattern - } - nchunk = chunk[n:] - if len(nchunk) == 0 { - err = ErrBadPattern - } - return -} - -// Split splits path immediately following the final Separator, -// separating it into a directory and file name component. -// If there is no Separator in path, Split returns an empty dir -// and file set to path. -// The returned values have the property that path = dir+file. -func Split(path string) (dir, file string) { - i := len(path) - 1 - for i >= 0 && !isPathSeparator(path[i]) { - i-- - } - return path[:i+1], path[i+1:] -} - -// Glob returns the names of all files matching pattern or nil -// if there is no matching file. The syntax of patterns is the same -// as in Match. The pattern may describe hierarchical names such as -// /usr/*/bin/ed (assuming the Separator is '/'). -// -// Glob ignores file system errors such as I/O errors reading directories. -// The only possible returned error is ErrBadPattern, when pattern -// is malformed. -func (c *Client) Glob(pattern string) (matches []string, err error) { - if !hasMeta(pattern) { - file, err := c.Lstat(pattern) - if err != nil { - return nil, nil - } - dir, _ := Split(pattern) - dir = cleanGlobPath(dir) - return []string{Join(dir, file.Name())}, nil - } - - dir, file := Split(pattern) - dir = cleanGlobPath(dir) - - if !hasMeta(dir) { - return c.glob(dir, file, nil) - } - - // Prevent infinite recursion. See issue 15879. - if dir == pattern { - return nil, ErrBadPattern - } - - var m []string - m, err = c.Glob(dir) - if err != nil { - return - } - for _, d := range m { - matches, err = c.glob(d, file, matches) - if err != nil { - return - } - } - return -} - -// cleanGlobPath prepares path for glob matching. -func cleanGlobPath(path string) string { - switch path { - case "": - return "." - case string(separator): - // do nothing to the path - return path - default: - return path[0 : len(path)-1] // chop off trailing separator - } -} - -// glob searches for files matching pattern in the directory dir -// and appends them to matches. If the directory cannot be -// opened, it returns the existing matches. New matches are -// added in lexicographical order. -func (c *Client) glob(dir, pattern string, matches []string) (m []string, e error) { - m = matches - fi, err := c.Stat(dir) - if err != nil { - return - } - if !fi.IsDir() { - return - } - names, err := c.ReadDir(dir) - if err != nil { - return - } - //sort.Strings(names) - - for _, n := range names { - matched, err := Match(pattern, n.Name()) - if err != nil { - return m, err - } - if matched { - m = append(m, Join(dir, n.Name())) - } - } - return -} - -// Join joins any number of path elements into a single path, adding -// a Separator if necessary. -// all empty strings are ignored. -func Join(elem ...string) string { - return path.Join(elem...) -} - -// hasMeta reports whether path contains any of the magic characters -// recognized by Match. -func hasMeta(path string) bool { - // TODO(niemeyer): Should other magic characters be added here? - return strings.ContainsAny(path, "*?[") -} diff --git a/vendor/github.com/pkg/sftp/packet-manager.go b/vendor/github.com/pkg/sftp/packet-manager.go deleted file mode 100644 index d47713b5d97..00000000000 --- a/vendor/github.com/pkg/sftp/packet-manager.go +++ /dev/null @@ -1,201 +0,0 @@ -package sftp - -import ( - "encoding" - "sort" - "sync" -) - -// The goal of the packetManager is to keep the outgoing packets in the same -// order as the incoming as is requires by section 7 of the RFC. - -type packetManager struct { - requests chan orderedPacket - responses chan orderedPacket - fini chan struct{} - incoming orderedPackets - outgoing orderedPackets - sender packetSender // connection object - working *sync.WaitGroup - packetCount uint32 -} - -type packetSender interface { - sendPacket(encoding.BinaryMarshaler) error -} - -func newPktMgr(sender packetSender) *packetManager { - s := &packetManager{ - requests: make(chan orderedPacket, SftpServerWorkerCount), - responses: make(chan orderedPacket, SftpServerWorkerCount), - fini: make(chan struct{}), - incoming: make([]orderedPacket, 0, SftpServerWorkerCount), - outgoing: make([]orderedPacket, 0, SftpServerWorkerCount), - sender: sender, - working: &sync.WaitGroup{}, - } - go s.controller() - return s -} - -//// packet ordering -func (s *packetManager) newOrderId() uint32 { - s.packetCount++ - return s.packetCount -} - -type orderedRequest struct { - requestPacket - orderid uint32 -} - -func (s *packetManager) newOrderedRequest(p requestPacket) orderedRequest { - return orderedRequest{requestPacket: p, orderid: s.newOrderId()} -} -func (p orderedRequest) orderId() uint32 { return p.orderid } -func (p orderedRequest) setOrderId(oid uint32) { p.orderid = oid } - -type orderedResponse struct { - responsePacket - orderid uint32 -} - -func (s *packetManager) newOrderedResponse(p responsePacket, id uint32, -) orderedResponse { - return orderedResponse{responsePacket: p, orderid: id} -} -func (p orderedResponse) orderId() uint32 { return p.orderid } -func (p orderedResponse) setOrderId(oid uint32) { p.orderid = oid } - -type orderedPacket interface { - id() uint32 - orderId() uint32 -} -type orderedPackets []orderedPacket - -func (o orderedPackets) Sort() { - sort.Slice(o, func(i, j int) bool { - return o[i].orderId() < o[j].orderId() - }) -} - -//// packet registry -// register incoming packets to be handled -func (s *packetManager) incomingPacket(pkt orderedRequest) { - s.working.Add(1) - s.requests <- pkt -} - -// register outgoing packets as being ready -func (s *packetManager) readyPacket(pkt orderedResponse) { - s.responses <- pkt - s.working.Done() -} - -// shut down packetManager controller -func (s *packetManager) close() { - // pause until current packets are processed - s.working.Wait() - close(s.fini) -} - -// Passed a worker function, returns a channel for incoming packets. -// Keep process packet responses in the order they are received while -// maximizing throughput of file transfers. -func (s *packetManager) workerChan(runWorker func(chan orderedRequest), -) chan orderedRequest { - - // multiple workers for faster read/writes - rwChan := make(chan orderedRequest, SftpServerWorkerCount) - for i := 0; i < SftpServerWorkerCount; i++ { - runWorker(rwChan) - } - - // single worker to enforce sequential processing of everything else - cmdChan := make(chan orderedRequest) - runWorker(cmdChan) - - pktChan := make(chan orderedRequest, SftpServerWorkerCount) - go func() { - for pkt := range pktChan { - switch pkt.requestPacket.(type) { - case *sshFxpReadPacket, *sshFxpWritePacket: - s.incomingPacket(pkt) - rwChan <- pkt - continue - case *sshFxpClosePacket: - // wait for reads/writes to finish when file is closed - // incomingPacket() call must occur after this - s.working.Wait() - } - s.incomingPacket(pkt) - // all non-RW use sequential cmdChan - cmdChan <- pkt - } - close(rwChan) - close(cmdChan) - s.close() - }() - - return pktChan -} - -// process packets -func (s *packetManager) controller() { - for { - select { - case pkt := <-s.requests: - debug("incoming id (oid): %v (%v)", pkt.id(), pkt.orderId()) - s.incoming = append(s.incoming, pkt) - s.incoming.Sort() - case pkt := <-s.responses: - debug("outgoing id (oid): %v (%v)", pkt.id(), pkt.orderId()) - s.outgoing = append(s.outgoing, pkt) - s.outgoing.Sort() - case <-s.fini: - return - } - s.maybeSendPackets() - } -} - -// send as many packets as are ready -func (s *packetManager) maybeSendPackets() { - for { - if len(s.outgoing) == 0 || len(s.incoming) == 0 { - debug("break! -- outgoing: %v; incoming: %v", - len(s.outgoing), len(s.incoming)) - break - } - out := s.outgoing[0] - in := s.incoming[0] - // debug("incoming: %v", ids(s.incoming)) - // debug("outgoing: %v", ids(s.outgoing)) - if in.orderId() == out.orderId() { - debug("Sending packet: %v", out.id()) - s.sender.sendPacket(out.(encoding.BinaryMarshaler)) - // pop off heads - copy(s.incoming, s.incoming[1:]) // shift left - s.incoming = s.incoming[:len(s.incoming)-1] // remove last - copy(s.outgoing, s.outgoing[1:]) // shift left - s.outgoing = s.outgoing[:len(s.outgoing)-1] // remove last - } else { - break - } - } -} - -// func oids(o []orderedPacket) []uint32 { -// res := make([]uint32, 0, len(o)) -// for _, v := range o { -// res = append(res, v.orderId()) -// } -// return res -// } -// func ids(o []orderedPacket) []uint32 { -// res := make([]uint32, 0, len(o)) -// for _, v := range o { -// res = append(res, v.id()) -// } -// return res -// } diff --git a/vendor/github.com/pkg/sftp/packet-typing.go b/vendor/github.com/pkg/sftp/packet-typing.go deleted file mode 100644 index bcff9bf339e..00000000000 --- a/vendor/github.com/pkg/sftp/packet-typing.go +++ /dev/null @@ -1,134 +0,0 @@ -package sftp - -import ( - "encoding" - - "github.com/pkg/errors" -) - -// all incoming packets -type requestPacket interface { - encoding.BinaryUnmarshaler - id() uint32 -} - -type responsePacket interface { - encoding.BinaryMarshaler - id() uint32 -} - -// interfaces to group types -type hasPath interface { - requestPacket - getPath() string -} - -type hasHandle interface { - requestPacket - getHandle() string -} - -type notReadOnly interface { - notReadOnly() -} - -//// define types by adding methods -// hasPath -func (p sshFxpLstatPacket) getPath() string { return p.Path } -func (p sshFxpStatPacket) getPath() string { return p.Path } -func (p sshFxpRmdirPacket) getPath() string { return p.Path } -func (p sshFxpReadlinkPacket) getPath() string { return p.Path } -func (p sshFxpRealpathPacket) getPath() string { return p.Path } -func (p sshFxpMkdirPacket) getPath() string { return p.Path } -func (p sshFxpSetstatPacket) getPath() string { return p.Path } -func (p sshFxpStatvfsPacket) getPath() string { return p.Path } -func (p sshFxpRemovePacket) getPath() string { return p.Filename } -func (p sshFxpRenamePacket) getPath() string { return p.Oldpath } -func (p sshFxpSymlinkPacket) getPath() string { return p.Targetpath } -func (p sshFxpOpendirPacket) getPath() string { return p.Path } -func (p sshFxpOpenPacket) getPath() string { return p.Path } - -func (p sshFxpExtendedPacketPosixRename) getPath() string { return p.Oldpath } - -// hasHandle -func (p sshFxpFstatPacket) getHandle() string { return p.Handle } -func (p sshFxpFsetstatPacket) getHandle() string { return p.Handle } -func (p sshFxpReadPacket) getHandle() string { return p.Handle } -func (p sshFxpWritePacket) getHandle() string { return p.Handle } -func (p sshFxpReaddirPacket) getHandle() string { return p.Handle } -func (p sshFxpClosePacket) getHandle() string { return p.Handle } - -// notReadOnly -func (p sshFxpWritePacket) notReadOnly() {} -func (p sshFxpSetstatPacket) notReadOnly() {} -func (p sshFxpFsetstatPacket) notReadOnly() {} -func (p sshFxpRemovePacket) notReadOnly() {} -func (p sshFxpMkdirPacket) notReadOnly() {} -func (p sshFxpRmdirPacket) notReadOnly() {} -func (p sshFxpRenamePacket) notReadOnly() {} -func (p sshFxpSymlinkPacket) notReadOnly() {} -func (p sshFxpExtendedPacketPosixRename) notReadOnly() {} - -// some packets with ID are missing id() -func (p sshFxpDataPacket) id() uint32 { return p.ID } -func (p sshFxpStatusPacket) id() uint32 { return p.ID } -func (p sshFxpStatResponse) id() uint32 { return p.ID } -func (p sshFxpNamePacket) id() uint32 { return p.ID } -func (p sshFxpHandlePacket) id() uint32 { return p.ID } -func (p StatVFS) id() uint32 { return p.ID } -func (p sshFxVersionPacket) id() uint32 { return 0 } - -// take raw incoming packet data and build packet objects -func makePacket(p rxPacket) (requestPacket, error) { - var pkt requestPacket - switch p.pktType { - case ssh_FXP_INIT: - pkt = &sshFxInitPacket{} - case ssh_FXP_LSTAT: - pkt = &sshFxpLstatPacket{} - case ssh_FXP_OPEN: - pkt = &sshFxpOpenPacket{} - case ssh_FXP_CLOSE: - pkt = &sshFxpClosePacket{} - case ssh_FXP_READ: - pkt = &sshFxpReadPacket{} - case ssh_FXP_WRITE: - pkt = &sshFxpWritePacket{} - case ssh_FXP_FSTAT: - pkt = &sshFxpFstatPacket{} - case ssh_FXP_SETSTAT: - pkt = &sshFxpSetstatPacket{} - case ssh_FXP_FSETSTAT: - pkt = &sshFxpFsetstatPacket{} - case ssh_FXP_OPENDIR: - pkt = &sshFxpOpendirPacket{} - case ssh_FXP_READDIR: - pkt = &sshFxpReaddirPacket{} - case ssh_FXP_REMOVE: - pkt = &sshFxpRemovePacket{} - case ssh_FXP_MKDIR: - pkt = &sshFxpMkdirPacket{} - case ssh_FXP_RMDIR: - pkt = &sshFxpRmdirPacket{} - case ssh_FXP_REALPATH: - pkt = &sshFxpRealpathPacket{} - case ssh_FXP_STAT: - pkt = &sshFxpStatPacket{} - case ssh_FXP_RENAME: - pkt = &sshFxpRenamePacket{} - case ssh_FXP_READLINK: - pkt = &sshFxpReadlinkPacket{} - case ssh_FXP_SYMLINK: - pkt = &sshFxpSymlinkPacket{} - case ssh_FXP_EXTENDED: - pkt = &sshFxpExtendedPacket{} - default: - return nil, errors.Errorf("unhandled packet type: %s", p.pktType) - } - if err := pkt.UnmarshalBinary(p.pktBytes); err != nil { - // Return partially unpacked packet to allow callers to return - // error messages appropriately with necessary id() method. - return pkt, err - } - return pkt, nil -} diff --git a/vendor/github.com/pkg/sftp/packet.go b/vendor/github.com/pkg/sftp/packet.go deleted file mode 100644 index e2dec7aeaed..00000000000 --- a/vendor/github.com/pkg/sftp/packet.go +++ /dev/null @@ -1,959 +0,0 @@ -package sftp - -import ( - "bytes" - "encoding" - "encoding/binary" - "fmt" - "io" - "os" - "reflect" - - "github.com/pkg/errors" -) - -var ( - errShortPacket = errors.New("packet too short") - errUnknownExtendedPacket = errors.New("unknown extended packet") -) - -const ( - debugDumpTxPacket = false - debugDumpRxPacket = false - debugDumpTxPacketBytes = false - debugDumpRxPacketBytes = false -) - -func marshalUint32(b []byte, v uint32) []byte { - return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v)) -} - -func marshalUint64(b []byte, v uint64) []byte { - return marshalUint32(marshalUint32(b, uint32(v>>32)), uint32(v)) -} - -func marshalString(b []byte, v string) []byte { - return append(marshalUint32(b, uint32(len(v))), v...) -} - -func marshal(b []byte, v interface{}) []byte { - if v == nil { - return b - } - switch v := v.(type) { - case uint8: - return append(b, v) - case uint32: - return marshalUint32(b, v) - case uint64: - return marshalUint64(b, v) - case string: - return marshalString(b, v) - case os.FileInfo: - return marshalFileInfo(b, v) - default: - switch d := reflect.ValueOf(v); d.Kind() { - case reflect.Struct: - for i, n := 0, d.NumField(); i < n; i++ { - b = append(marshal(b, d.Field(i).Interface())) - } - return b - case reflect.Slice: - for i, n := 0, d.Len(); i < n; i++ { - b = append(marshal(b, d.Index(i).Interface())) - } - return b - default: - panic(fmt.Sprintf("marshal(%#v): cannot handle type %T", v, v)) - } - } -} - -func unmarshalUint32(b []byte) (uint32, []byte) { - v := uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24 - return v, b[4:] -} - -func unmarshalUint32Safe(b []byte) (uint32, []byte, error) { - var v uint32 - if len(b) < 4 { - return 0, nil, errShortPacket - } - v, b = unmarshalUint32(b) - return v, b, nil -} - -func unmarshalUint64(b []byte) (uint64, []byte) { - h, b := unmarshalUint32(b) - l, b := unmarshalUint32(b) - return uint64(h)<<32 | uint64(l), b -} - -func unmarshalUint64Safe(b []byte) (uint64, []byte, error) { - var v uint64 - if len(b) < 8 { - return 0, nil, errShortPacket - } - v, b = unmarshalUint64(b) - return v, b, nil -} - -func unmarshalString(b []byte) (string, []byte) { - n, b := unmarshalUint32(b) - return string(b[:n]), b[n:] -} - -func unmarshalStringSafe(b []byte) (string, []byte, error) { - n, b, err := unmarshalUint32Safe(b) - if err != nil { - return "", nil, err - } - if int64(n) > int64(len(b)) { - return "", nil, errShortPacket - } - return string(b[:n]), b[n:], nil -} - -// sendPacket marshals p according to RFC 4234. -func sendPacket(w io.Writer, m encoding.BinaryMarshaler) error { - bb, err := m.MarshalBinary() - if err != nil { - return errors.Errorf("binary marshaller failed: %v", err) - } - if debugDumpTxPacketBytes { - debug("send packet: %s %d bytes %x", fxp(bb[0]), len(bb), bb[1:]) - } else if debugDumpTxPacket { - debug("send packet: %s %d bytes", fxp(bb[0]), len(bb)) - } - // Slide packet down 4 bytes to make room for length header. - packet := append(bb, make([]byte, 4)...) // optimistically assume bb has capacity - copy(packet[4:], bb) - binary.BigEndian.PutUint32(packet[:4], uint32(len(bb))) - - _, err = w.Write(packet) - if err != nil { - return errors.Errorf("failed to send packet: %v", err) - } - return nil -} - -func recvPacket(r io.Reader) (uint8, []byte, error) { - var b = []byte{0, 0, 0, 0} - if _, err := io.ReadFull(r, b); err != nil { - return 0, nil, err - } - l, _ := unmarshalUint32(b) - b = make([]byte, l) - if _, err := io.ReadFull(r, b); err != nil { - debug("recv packet %d bytes: err %v", l, err) - return 0, nil, err - } - if debugDumpRxPacketBytes { - debug("recv packet: %s %d bytes %x", fxp(b[0]), l, b[1:]) - } else if debugDumpRxPacket { - debug("recv packet: %s %d bytes", fxp(b[0]), l) - } - return b[0], b[1:], nil -} - -type extensionPair struct { - Name string - Data string -} - -func unmarshalExtensionPair(b []byte) (extensionPair, []byte, error) { - var ep extensionPair - var err error - ep.Name, b, err = unmarshalStringSafe(b) - if err != nil { - return ep, b, err - } - ep.Data, b, err = unmarshalStringSafe(b) - return ep, b, err -} - -// Here starts the definition of packets along with their MarshalBinary -// implementations. -// Manually writing the marshalling logic wins us a lot of time and -// allocation. - -type sshFxInitPacket struct { - Version uint32 - Extensions []extensionPair -} - -func (p sshFxInitPacket) MarshalBinary() ([]byte, error) { - l := 1 + 4 // byte + uint32 - for _, e := range p.Extensions { - l += 4 + len(e.Name) + 4 + len(e.Data) - } - - b := make([]byte, 0, l) - b = append(b, ssh_FXP_INIT) - b = marshalUint32(b, p.Version) - for _, e := range p.Extensions { - b = marshalString(b, e.Name) - b = marshalString(b, e.Data) - } - return b, nil -} - -func (p *sshFxInitPacket) UnmarshalBinary(b []byte) error { - var err error - if p.Version, b, err = unmarshalUint32Safe(b); err != nil { - return err - } - for len(b) > 0 { - var ep extensionPair - ep, b, err = unmarshalExtensionPair(b) - if err != nil { - return err - } - p.Extensions = append(p.Extensions, ep) - } - return nil -} - -type sshFxVersionPacket struct { - Version uint32 - Extensions []struct { - Name, Data string - } -} - -func (p sshFxVersionPacket) MarshalBinary() ([]byte, error) { - l := 1 + 4 // byte + uint32 - for _, e := range p.Extensions { - l += 4 + len(e.Name) + 4 + len(e.Data) - } - - b := make([]byte, 0, l) - b = append(b, ssh_FXP_VERSION) - b = marshalUint32(b, p.Version) - for _, e := range p.Extensions { - b = marshalString(b, e.Name) - b = marshalString(b, e.Data) - } - return b, nil -} - -func marshalIDString(packetType byte, id uint32, str string) ([]byte, error) { - l := 1 + 4 + // type(byte) + uint32 - 4 + len(str) - - b := make([]byte, 0, l) - b = append(b, packetType) - b = marshalUint32(b, id) - b = marshalString(b, str) - return b, nil -} - -func unmarshalIDString(b []byte, id *uint32, str *string) error { - var err error - *id, b, err = unmarshalUint32Safe(b) - if err != nil { - return err - } - *str, _, err = unmarshalStringSafe(b) - return err -} - -type sshFxpReaddirPacket struct { - ID uint32 - Handle string -} - -func (p sshFxpReaddirPacket) id() uint32 { return p.ID } - -func (p sshFxpReaddirPacket) MarshalBinary() ([]byte, error) { - return marshalIDString(ssh_FXP_READDIR, p.ID, p.Handle) -} - -func (p *sshFxpReaddirPacket) UnmarshalBinary(b []byte) error { - return unmarshalIDString(b, &p.ID, &p.Handle) -} - -type sshFxpOpendirPacket struct { - ID uint32 - Path string -} - -func (p sshFxpOpendirPacket) id() uint32 { return p.ID } - -func (p sshFxpOpendirPacket) MarshalBinary() ([]byte, error) { - return marshalIDString(ssh_FXP_OPENDIR, p.ID, p.Path) -} - -func (p *sshFxpOpendirPacket) UnmarshalBinary(b []byte) error { - return unmarshalIDString(b, &p.ID, &p.Path) -} - -type sshFxpLstatPacket struct { - ID uint32 - Path string -} - -func (p sshFxpLstatPacket) id() uint32 { return p.ID } - -func (p sshFxpLstatPacket) MarshalBinary() ([]byte, error) { - return marshalIDString(ssh_FXP_LSTAT, p.ID, p.Path) -} - -func (p *sshFxpLstatPacket) UnmarshalBinary(b []byte) error { - return unmarshalIDString(b, &p.ID, &p.Path) -} - -type sshFxpStatPacket struct { - ID uint32 - Path string -} - -func (p sshFxpStatPacket) id() uint32 { return p.ID } - -func (p sshFxpStatPacket) MarshalBinary() ([]byte, error) { - return marshalIDString(ssh_FXP_STAT, p.ID, p.Path) -} - -func (p *sshFxpStatPacket) UnmarshalBinary(b []byte) error { - return unmarshalIDString(b, &p.ID, &p.Path) -} - -type sshFxpFstatPacket struct { - ID uint32 - Handle string -} - -func (p sshFxpFstatPacket) id() uint32 { return p.ID } - -func (p sshFxpFstatPacket) MarshalBinary() ([]byte, error) { - return marshalIDString(ssh_FXP_FSTAT, p.ID, p.Handle) -} - -func (p *sshFxpFstatPacket) UnmarshalBinary(b []byte) error { - return unmarshalIDString(b, &p.ID, &p.Handle) -} - -type sshFxpClosePacket struct { - ID uint32 - Handle string -} - -func (p sshFxpClosePacket) id() uint32 { return p.ID } - -func (p sshFxpClosePacket) MarshalBinary() ([]byte, error) { - return marshalIDString(ssh_FXP_CLOSE, p.ID, p.Handle) -} - -func (p *sshFxpClosePacket) UnmarshalBinary(b []byte) error { - return unmarshalIDString(b, &p.ID, &p.Handle) -} - -type sshFxpRemovePacket struct { - ID uint32 - Filename string -} - -func (p sshFxpRemovePacket) id() uint32 { return p.ID } - -func (p sshFxpRemovePacket) MarshalBinary() ([]byte, error) { - return marshalIDString(ssh_FXP_REMOVE, p.ID, p.Filename) -} - -func (p *sshFxpRemovePacket) UnmarshalBinary(b []byte) error { - return unmarshalIDString(b, &p.ID, &p.Filename) -} - -type sshFxpRmdirPacket struct { - ID uint32 - Path string -} - -func (p sshFxpRmdirPacket) id() uint32 { return p.ID } - -func (p sshFxpRmdirPacket) MarshalBinary() ([]byte, error) { - return marshalIDString(ssh_FXP_RMDIR, p.ID, p.Path) -} - -func (p *sshFxpRmdirPacket) UnmarshalBinary(b []byte) error { - return unmarshalIDString(b, &p.ID, &p.Path) -} - -type sshFxpSymlinkPacket struct { - ID uint32 - Targetpath string - Linkpath string -} - -func (p sshFxpSymlinkPacket) id() uint32 { return p.ID } - -func (p sshFxpSymlinkPacket) MarshalBinary() ([]byte, error) { - l := 1 + 4 + // type(byte) + uint32 - 4 + len(p.Targetpath) + - 4 + len(p.Linkpath) - - b := make([]byte, 0, l) - b = append(b, ssh_FXP_SYMLINK) - b = marshalUint32(b, p.ID) - b = marshalString(b, p.Targetpath) - b = marshalString(b, p.Linkpath) - return b, nil -} - -func (p *sshFxpSymlinkPacket) UnmarshalBinary(b []byte) error { - var err error - if p.ID, b, err = unmarshalUint32Safe(b); err != nil { - return err - } else if p.Targetpath, b, err = unmarshalStringSafe(b); err != nil { - return err - } else if p.Linkpath, _, err = unmarshalStringSafe(b); err != nil { - return err - } - return nil -} - -type sshFxpReadlinkPacket struct { - ID uint32 - Path string -} - -func (p sshFxpReadlinkPacket) id() uint32 { return p.ID } - -func (p sshFxpReadlinkPacket) MarshalBinary() ([]byte, error) { - return marshalIDString(ssh_FXP_READLINK, p.ID, p.Path) -} - -func (p *sshFxpReadlinkPacket) UnmarshalBinary(b []byte) error { - return unmarshalIDString(b, &p.ID, &p.Path) -} - -type sshFxpRealpathPacket struct { - ID uint32 - Path string -} - -func (p sshFxpRealpathPacket) id() uint32 { return p.ID } - -func (p sshFxpRealpathPacket) MarshalBinary() ([]byte, error) { - return marshalIDString(ssh_FXP_REALPATH, p.ID, p.Path) -} - -func (p *sshFxpRealpathPacket) UnmarshalBinary(b []byte) error { - return unmarshalIDString(b, &p.ID, &p.Path) -} - -type sshFxpNameAttr struct { - Name string - LongName string - Attrs []interface{} -} - -func (p sshFxpNameAttr) MarshalBinary() ([]byte, error) { - b := []byte{} - b = marshalString(b, p.Name) - b = marshalString(b, p.LongName) - for _, attr := range p.Attrs { - b = marshal(b, attr) - } - return b, nil -} - -type sshFxpNamePacket struct { - ID uint32 - NameAttrs []sshFxpNameAttr -} - -func (p sshFxpNamePacket) MarshalBinary() ([]byte, error) { - b := []byte{} - b = append(b, ssh_FXP_NAME) - b = marshalUint32(b, p.ID) - b = marshalUint32(b, uint32(len(p.NameAttrs))) - for _, na := range p.NameAttrs { - ab, err := na.MarshalBinary() - if err != nil { - return nil, err - } - - b = append(b, ab...) - } - return b, nil -} - -type sshFxpOpenPacket struct { - ID uint32 - Path string - Pflags uint32 - Flags uint32 // ignored -} - -func (p sshFxpOpenPacket) id() uint32 { return p.ID } - -func (p sshFxpOpenPacket) MarshalBinary() ([]byte, error) { - l := 1 + 4 + - 4 + len(p.Path) + - 4 + 4 - - b := make([]byte, 0, l) - b = append(b, ssh_FXP_OPEN) - b = marshalUint32(b, p.ID) - b = marshalString(b, p.Path) - b = marshalUint32(b, p.Pflags) - b = marshalUint32(b, p.Flags) - return b, nil -} - -func (p *sshFxpOpenPacket) UnmarshalBinary(b []byte) error { - var err error - if p.ID, b, err = unmarshalUint32Safe(b); err != nil { - return err - } else if p.Path, b, err = unmarshalStringSafe(b); err != nil { - return err - } else if p.Pflags, b, err = unmarshalUint32Safe(b); err != nil { - return err - } else if p.Flags, _, err = unmarshalUint32Safe(b); err != nil { - return err - } - return nil -} - -type sshFxpReadPacket struct { - ID uint32 - Handle string - Offset uint64 - Len uint32 -} - -func (p sshFxpReadPacket) id() uint32 { return p.ID } - -func (p sshFxpReadPacket) MarshalBinary() ([]byte, error) { - l := 1 + 4 + // type(byte) + uint32 - 4 + len(p.Handle) + - 8 + 4 // uint64 + uint32 - - b := make([]byte, 0, l) - b = append(b, ssh_FXP_READ) - b = marshalUint32(b, p.ID) - b = marshalString(b, p.Handle) - b = marshalUint64(b, p.Offset) - b = marshalUint32(b, p.Len) - return b, nil -} - -func (p *sshFxpReadPacket) UnmarshalBinary(b []byte) error { - var err error - if p.ID, b, err = unmarshalUint32Safe(b); err != nil { - return err - } else if p.Handle, b, err = unmarshalStringSafe(b); err != nil { - return err - } else if p.Offset, b, err = unmarshalUint64Safe(b); err != nil { - return err - } else if p.Len, _, err = unmarshalUint32Safe(b); err != nil { - return err - } - return nil -} - -type sshFxpRenamePacket struct { - ID uint32 - Oldpath string - Newpath string -} - -func (p sshFxpRenamePacket) id() uint32 { return p.ID } - -func (p sshFxpRenamePacket) MarshalBinary() ([]byte, error) { - l := 1 + 4 + // type(byte) + uint32 - 4 + len(p.Oldpath) + - 4 + len(p.Newpath) - - b := make([]byte, 0, l) - b = append(b, ssh_FXP_RENAME) - b = marshalUint32(b, p.ID) - b = marshalString(b, p.Oldpath) - b = marshalString(b, p.Newpath) - return b, nil -} - -func (p *sshFxpRenamePacket) UnmarshalBinary(b []byte) error { - var err error - if p.ID, b, err = unmarshalUint32Safe(b); err != nil { - return err - } else if p.Oldpath, b, err = unmarshalStringSafe(b); err != nil { - return err - } else if p.Newpath, _, err = unmarshalStringSafe(b); err != nil { - return err - } - return nil -} - -type sshFxpPosixRenamePacket struct { - ID uint32 - Oldpath string - Newpath string -} - -func (p sshFxpPosixRenamePacket) id() uint32 { return p.ID } - -func (p sshFxpPosixRenamePacket) MarshalBinary() ([]byte, error) { - const ext = "posix-rename@openssh.com" - l := 1 + 4 + // type(byte) + uint32 - 4 + len(ext) + - 4 + len(p.Oldpath) + - 4 + len(p.Newpath) - - b := make([]byte, 0, l) - b = append(b, ssh_FXP_EXTENDED) - b = marshalUint32(b, p.ID) - b = marshalString(b, ext) - b = marshalString(b, p.Oldpath) - b = marshalString(b, p.Newpath) - return b, nil -} - -type sshFxpWritePacket struct { - ID uint32 - Handle string - Offset uint64 - Length uint32 - Data []byte -} - -func (p sshFxpWritePacket) id() uint32 { return p.ID } - -func (p sshFxpWritePacket) MarshalBinary() ([]byte, error) { - l := 1 + 4 + // type(byte) + uint32 - 4 + len(p.Handle) + - 8 + 4 + // uint64 + uint32 - len(p.Data) - - b := make([]byte, 0, l) - b = append(b, ssh_FXP_WRITE) - b = marshalUint32(b, p.ID) - b = marshalString(b, p.Handle) - b = marshalUint64(b, p.Offset) - b = marshalUint32(b, p.Length) - b = append(b, p.Data...) - return b, nil -} - -func (p *sshFxpWritePacket) UnmarshalBinary(b []byte) error { - var err error - if p.ID, b, err = unmarshalUint32Safe(b); err != nil { - return err - } else if p.Handle, b, err = unmarshalStringSafe(b); err != nil { - return err - } else if p.Offset, b, err = unmarshalUint64Safe(b); err != nil { - return err - } else if p.Length, b, err = unmarshalUint32Safe(b); err != nil { - return err - } else if uint32(len(b)) < p.Length { - return errShortPacket - } - - p.Data = append([]byte{}, b[:p.Length]...) - return nil -} - -type sshFxpMkdirPacket struct { - ID uint32 - Path string - Flags uint32 // ignored -} - -func (p sshFxpMkdirPacket) id() uint32 { return p.ID } - -func (p sshFxpMkdirPacket) MarshalBinary() ([]byte, error) { - l := 1 + 4 + // type(byte) + uint32 - 4 + len(p.Path) + - 4 // uint32 - - b := make([]byte, 0, l) - b = append(b, ssh_FXP_MKDIR) - b = marshalUint32(b, p.ID) - b = marshalString(b, p.Path) - b = marshalUint32(b, p.Flags) - return b, nil -} - -func (p *sshFxpMkdirPacket) UnmarshalBinary(b []byte) error { - var err error - if p.ID, b, err = unmarshalUint32Safe(b); err != nil { - return err - } else if p.Path, b, err = unmarshalStringSafe(b); err != nil { - return err - } else if p.Flags, _, err = unmarshalUint32Safe(b); err != nil { - return err - } - return nil -} - -type sshFxpSetstatPacket struct { - ID uint32 - Path string - Flags uint32 - Attrs interface{} -} - -type sshFxpFsetstatPacket struct { - ID uint32 - Handle string - Flags uint32 - Attrs interface{} -} - -func (p sshFxpSetstatPacket) id() uint32 { return p.ID } -func (p sshFxpFsetstatPacket) id() uint32 { return p.ID } - -func (p sshFxpSetstatPacket) MarshalBinary() ([]byte, error) { - l := 1 + 4 + // type(byte) + uint32 - 4 + len(p.Path) + - 4 // uint32 + uint64 - - b := make([]byte, 0, l) - b = append(b, ssh_FXP_SETSTAT) - b = marshalUint32(b, p.ID) - b = marshalString(b, p.Path) - b = marshalUint32(b, p.Flags) - b = marshal(b, p.Attrs) - return b, nil -} - -func (p sshFxpFsetstatPacket) MarshalBinary() ([]byte, error) { - l := 1 + 4 + // type(byte) + uint32 - 4 + len(p.Handle) + - 4 // uint32 + uint64 - - b := make([]byte, 0, l) - b = append(b, ssh_FXP_FSETSTAT) - b = marshalUint32(b, p.ID) - b = marshalString(b, p.Handle) - b = marshalUint32(b, p.Flags) - b = marshal(b, p.Attrs) - return b, nil -} - -func (p *sshFxpSetstatPacket) UnmarshalBinary(b []byte) error { - var err error - if p.ID, b, err = unmarshalUint32Safe(b); err != nil { - return err - } else if p.Path, b, err = unmarshalStringSafe(b); err != nil { - return err - } else if p.Flags, b, err = unmarshalUint32Safe(b); err != nil { - return err - } - p.Attrs = b - return nil -} - -func (p *sshFxpFsetstatPacket) UnmarshalBinary(b []byte) error { - var err error - if p.ID, b, err = unmarshalUint32Safe(b); err != nil { - return err - } else if p.Handle, b, err = unmarshalStringSafe(b); err != nil { - return err - } else if p.Flags, b, err = unmarshalUint32Safe(b); err != nil { - return err - } - p.Attrs = b - return nil -} - -type sshFxpHandlePacket struct { - ID uint32 - Handle string -} - -func (p sshFxpHandlePacket) MarshalBinary() ([]byte, error) { - b := []byte{ssh_FXP_HANDLE} - b = marshalUint32(b, p.ID) - b = marshalString(b, p.Handle) - return b, nil -} - -type sshFxpStatusPacket struct { - ID uint32 - StatusError -} - -func (p sshFxpStatusPacket) MarshalBinary() ([]byte, error) { - b := []byte{ssh_FXP_STATUS} - b = marshalUint32(b, p.ID) - b = marshalStatus(b, p.StatusError) - return b, nil -} - -type sshFxpDataPacket struct { - ID uint32 - Length uint32 - Data []byte -} - -func (p sshFxpDataPacket) MarshalBinary() ([]byte, error) { - b := []byte{ssh_FXP_DATA} - b = marshalUint32(b, p.ID) - b = marshalUint32(b, p.Length) - b = append(b, p.Data[:p.Length]...) - return b, nil -} - -func (p *sshFxpDataPacket) UnmarshalBinary(b []byte) error { - var err error - if p.ID, b, err = unmarshalUint32Safe(b); err != nil { - return err - } else if p.Length, b, err = unmarshalUint32Safe(b); err != nil { - return err - } else if uint32(len(b)) < p.Length { - return errors.New("truncated packet") - } - - p.Data = make([]byte, p.Length) - copy(p.Data, b) - return nil -} - -type sshFxpStatvfsPacket struct { - ID uint32 - Path string -} - -func (p sshFxpStatvfsPacket) id() uint32 { return p.ID } - -func (p sshFxpStatvfsPacket) MarshalBinary() ([]byte, error) { - l := 1 + 4 + // type(byte) + uint32 - len(p.Path) + - len("statvfs@openssh.com") - - b := make([]byte, 0, l) - b = append(b, ssh_FXP_EXTENDED) - b = marshalUint32(b, p.ID) - b = marshalString(b, "statvfs@openssh.com") - b = marshalString(b, p.Path) - return b, nil -} - -// A StatVFS contains statistics about a filesystem. -type StatVFS struct { - ID uint32 - Bsize uint64 /* file system block size */ - Frsize uint64 /* fundamental fs block size */ - Blocks uint64 /* number of blocks (unit f_frsize) */ - Bfree uint64 /* free blocks in file system */ - Bavail uint64 /* free blocks for non-root */ - Files uint64 /* total file inodes */ - Ffree uint64 /* free file inodes */ - Favail uint64 /* free file inodes for to non-root */ - Fsid uint64 /* file system id */ - Flag uint64 /* bit mask of f_flag values */ - Namemax uint64 /* maximum filename length */ -} - -// TotalSpace calculates the amount of total space in a filesystem. -func (p *StatVFS) TotalSpace() uint64 { - return p.Frsize * p.Blocks -} - -// FreeSpace calculates the amount of free space in a filesystem. -func (p *StatVFS) FreeSpace() uint64 { - return p.Frsize * p.Bfree -} - -// Convert to ssh_FXP_EXTENDED_REPLY packet binary format -func (p *StatVFS) MarshalBinary() ([]byte, error) { - var buf bytes.Buffer - buf.Write([]byte{ssh_FXP_EXTENDED_REPLY}) - err := binary.Write(&buf, binary.BigEndian, p) - return buf.Bytes(), err -} - -type sshFxpExtendedPacket struct { - ID uint32 - ExtendedRequest string - SpecificPacket interface { - serverRespondablePacket - readonly() bool - } -} - -func (p sshFxpExtendedPacket) id() uint32 { return p.ID } -func (p sshFxpExtendedPacket) readonly() bool { - if p.SpecificPacket == nil { - return true - } - return p.SpecificPacket.readonly() -} - -func (p sshFxpExtendedPacket) respond(svr *Server) responsePacket { - if p.SpecificPacket == nil { - return statusFromError(p, nil) - } - return p.SpecificPacket.respond(svr) -} - -func (p *sshFxpExtendedPacket) UnmarshalBinary(b []byte) error { - var err error - bOrig := b - if p.ID, b, err = unmarshalUint32Safe(b); err != nil { - return err - } else if p.ExtendedRequest, _, err = unmarshalStringSafe(b); err != nil { - return err - } - - // specific unmarshalling - switch p.ExtendedRequest { - case "statvfs@openssh.com": - p.SpecificPacket = &sshFxpExtendedPacketStatVFS{} - case "posix-rename@openssh.com": - p.SpecificPacket = &sshFxpExtendedPacketPosixRename{} - default: - return errors.Wrapf(errUnknownExtendedPacket, "packet type %v", p.SpecificPacket) - } - - return p.SpecificPacket.UnmarshalBinary(bOrig) -} - -type sshFxpExtendedPacketStatVFS struct { - ID uint32 - ExtendedRequest string - Path string -} - -func (p sshFxpExtendedPacketStatVFS) id() uint32 { return p.ID } -func (p sshFxpExtendedPacketStatVFS) readonly() bool { return true } -func (p *sshFxpExtendedPacketStatVFS) UnmarshalBinary(b []byte) error { - var err error - if p.ID, b, err = unmarshalUint32Safe(b); err != nil { - return err - } else if p.ExtendedRequest, b, err = unmarshalStringSafe(b); err != nil { - return err - } else if p.Path, _, err = unmarshalStringSafe(b); err != nil { - return err - } - return nil -} - -type sshFxpExtendedPacketPosixRename struct { - ID uint32 - ExtendedRequest string - Oldpath string - Newpath string -} - -func (p sshFxpExtendedPacketPosixRename) id() uint32 { return p.ID } -func (p sshFxpExtendedPacketPosixRename) readonly() bool { return false } -func (p *sshFxpExtendedPacketPosixRename) UnmarshalBinary(b []byte) error { - var err error - if p.ID, b, err = unmarshalUint32Safe(b); err != nil { - return err - } else if p.ExtendedRequest, b, err = unmarshalStringSafe(b); err != nil { - return err - } else if p.Oldpath, b, err = unmarshalStringSafe(b); err != nil { - return err - } else if p.Newpath, _, err = unmarshalStringSafe(b); err != nil { - return err - } - return nil -} - -func (p sshFxpExtendedPacketPosixRename) respond(s *Server) responsePacket { - err := os.Rename(p.Oldpath, p.Newpath) - return statusFromError(p, err) -} diff --git a/vendor/github.com/pkg/sftp/release.go b/vendor/github.com/pkg/sftp/release.go deleted file mode 100644 index b695528fde1..00000000000 --- a/vendor/github.com/pkg/sftp/release.go +++ /dev/null @@ -1,5 +0,0 @@ -// +build !debug - -package sftp - -func debug(fmt string, args ...interface{}) {} diff --git a/vendor/github.com/pkg/sftp/request-attrs.go b/vendor/github.com/pkg/sftp/request-attrs.go deleted file mode 100644 index 31b1eaa6662..00000000000 --- a/vendor/github.com/pkg/sftp/request-attrs.go +++ /dev/null @@ -1,63 +0,0 @@ -package sftp - -// Methods on the Request object to make working with the Flags bitmasks and -// Attr(ibutes) byte blob easier. Use Pflags() when working with an Open/Write -// request and AttrFlags() and Attributes() when working with SetStat requests. -import "os" - -// File Open and Write Flags. Correlate directly with with os.OpenFile flags -// (https://golang.org/pkg/os/#pkg-constants). -type FileOpenFlags struct { - Read, Write, Append, Creat, Trunc, Excl bool -} - -func newFileOpenFlags(flags uint32) FileOpenFlags { - return FileOpenFlags{ - Read: flags&ssh_FXF_READ != 0, - Write: flags&ssh_FXF_WRITE != 0, - Append: flags&ssh_FXF_APPEND != 0, - Creat: flags&ssh_FXF_CREAT != 0, - Trunc: flags&ssh_FXF_TRUNC != 0, - Excl: flags&ssh_FXF_EXCL != 0, - } -} - -// Pflags converts the bitmap/uint32 from SFTP Open packet pflag values, -// into a FileOpenFlags struct with booleans set for flags set in bitmap. -func (r *Request) Pflags() FileOpenFlags { - return newFileOpenFlags(r.Flags) -} - -// Flags that indicate whether SFTP file attributes were passed. When a flag is -// true the corresponding attribute should be available from the FileStat -// object returned by Attributes method. Used with SetStat. -type FileAttrFlags struct { - Size, UidGid, Permissions, Acmodtime bool -} - -func newFileAttrFlags(flags uint32) FileAttrFlags { - return FileAttrFlags{ - Size: (flags & ssh_FILEXFER_ATTR_SIZE) != 0, - UidGid: (flags & ssh_FILEXFER_ATTR_UIDGID) != 0, - Permissions: (flags & ssh_FILEXFER_ATTR_PERMISSIONS) != 0, - Acmodtime: (flags & ssh_FILEXFER_ATTR_ACMODTIME) != 0, - } -} - -// FileAttrFlags returns a FileAttrFlags boolean struct based on the -// bitmap/uint32 file attribute flags from the SFTP packaet. -func (r *Request) AttrFlags() FileAttrFlags { - return newFileAttrFlags(r.Flags) -} - -// FileMode returns the Mode SFTP file attributes wrapped as os.FileMode -func (a FileStat) FileMode() os.FileMode { - return os.FileMode(a.Mode) -} - -// Attributres parses file attributes byte blob and return them in a -// FileStat object. -func (r *Request) Attributes() *FileStat { - fs, _ := getFileStat(r.Flags, r.Attrs) - return fs -} diff --git a/vendor/github.com/pkg/sftp/request-errors.go b/vendor/github.com/pkg/sftp/request-errors.go deleted file mode 100644 index 00451e74d7e..00000000000 --- a/vendor/github.com/pkg/sftp/request-errors.go +++ /dev/null @@ -1,42 +0,0 @@ -package sftp - -// Error types that match the SFTP's SSH_FXP_STATUS codes. Gives you more -// direct control of the errors being sent vs. letting the library work them -// out from the standard os/io errors. - -type fxerr uint32 - -const ( - ErrSshFxOk = fxerr(ssh_FX_OK) - ErrSshFxEof = fxerr(ssh_FX_EOF) - ErrSshFxNoSuchFile = fxerr(ssh_FX_NO_SUCH_FILE) - ErrSshFxPermissionDenied = fxerr(ssh_FX_PERMISSION_DENIED) - ErrSshFxFailure = fxerr(ssh_FX_FAILURE) - ErrSshFxBadMessage = fxerr(ssh_FX_BAD_MESSAGE) - ErrSshFxNoConnection = fxerr(ssh_FX_NO_CONNECTION) - ErrSshFxConnectionLost = fxerr(ssh_FX_CONNECTION_LOST) - ErrSshFxOpUnsupported = fxerr(ssh_FX_OP_UNSUPPORTED) -) - -func (e fxerr) Error() string { - switch e { - case ErrSshFxOk: - return "OK" - case ErrSshFxEof: - return "EOF" - case ErrSshFxNoSuchFile: - return "No Such File" - case ErrSshFxPermissionDenied: - return "Permission Denied" - case ErrSshFxBadMessage: - return "Bad Message" - case ErrSshFxNoConnection: - return "No Connection" - case ErrSshFxConnectionLost: - return "Connection Lost" - case ErrSshFxOpUnsupported: - return "Operation Unsupported" - default: - return "Failure" - } -} diff --git a/vendor/github.com/pkg/sftp/request-example.go b/vendor/github.com/pkg/sftp/request-example.go deleted file mode 100644 index 4ef2e67019b..00000000000 --- a/vendor/github.com/pkg/sftp/request-example.go +++ /dev/null @@ -1,262 +0,0 @@ -package sftp - -// This serves as an example of how to implement the request server handler as -// well as a dummy backend for testing. It implements an in-memory backend that -// works as a very simple filesystem with simple flat key-value lookup system. - -import ( - "bytes" - "fmt" - "io" - "os" - "path/filepath" - "sort" - "sync" - "time" -) - -// InMemHandler returns a Hanlders object with the test handlers. -func InMemHandler() Handlers { - root := &root{ - files: make(map[string]*memFile), - } - root.memFile = newMemFile("/", true) - return Handlers{root, root, root, root} -} - -// Example Handlers -func (fs *root) Fileread(r *Request) (io.ReaderAt, error) { - if fs.mockErr != nil { - return nil, fs.mockErr - } - fs.filesLock.Lock() - defer fs.filesLock.Unlock() - file, err := fs.fetch(r.Filepath) - if err != nil { - return nil, err - } - if file.symlink != "" { - file, err = fs.fetch(file.symlink) - if err != nil { - return nil, err - } - } - return file.ReaderAt() -} - -func (fs *root) Filewrite(r *Request) (io.WriterAt, error) { - if fs.mockErr != nil { - return nil, fs.mockErr - } - fs.filesLock.Lock() - defer fs.filesLock.Unlock() - file, err := fs.fetch(r.Filepath) - if err == os.ErrNotExist { - dir, err := fs.fetch(filepath.Dir(r.Filepath)) - if err != nil { - return nil, err - } - if !dir.isdir { - return nil, os.ErrInvalid - } - file = newMemFile(r.Filepath, false) - fs.files[r.Filepath] = file - } - return file.WriterAt() -} - -func (fs *root) Filecmd(r *Request) error { - if fs.mockErr != nil { - return fs.mockErr - } - fs.filesLock.Lock() - defer fs.filesLock.Unlock() - switch r.Method { - case "Setstat": - return nil - case "Rename": - file, err := fs.fetch(r.Filepath) - if err != nil { - return err - } - if _, ok := fs.files[r.Target]; ok { - return &os.LinkError{Op: "rename", Old: r.Filepath, New: r.Target, - Err: fmt.Errorf("dest file exists")} - } - file.name = r.Target - fs.files[r.Target] = file - delete(fs.files, r.Filepath) - case "Rmdir", "Remove": - _, err := fs.fetch(filepath.Dir(r.Filepath)) - if err != nil { - return err - } - delete(fs.files, r.Filepath) - case "Mkdir": - _, err := fs.fetch(filepath.Dir(r.Filepath)) - if err != nil { - return err - } - fs.files[r.Filepath] = newMemFile(r.Filepath, true) - case "Symlink": - _, err := fs.fetch(r.Filepath) - if err != nil { - return err - } - link := newMemFile(r.Target, false) - link.symlink = r.Filepath - fs.files[r.Target] = link - } - return nil -} - -type listerat []os.FileInfo - -// Modeled after strings.Reader's ReadAt() implementation -func (f listerat) ListAt(ls []os.FileInfo, offset int64) (int, error) { - var n int - if offset >= int64(len(f)) { - return 0, io.EOF - } - n = copy(ls, f[offset:]) - if n < len(ls) { - return n, io.EOF - } - return n, nil -} - -func (fs *root) Filelist(r *Request) (ListerAt, error) { - if fs.mockErr != nil { - return nil, fs.mockErr - } - fs.filesLock.Lock() - defer fs.filesLock.Unlock() - - switch r.Method { - case "List": - ordered_names := []string{} - for fn, _ := range fs.files { - if filepath.Dir(fn) == r.Filepath { - ordered_names = append(ordered_names, fn) - } - } - sort.Strings(ordered_names) - list := make([]os.FileInfo, len(ordered_names)) - for i, fn := range ordered_names { - list[i] = fs.files[fn] - } - return listerat(list), nil - case "Stat": - file, err := fs.fetch(r.Filepath) - if err != nil { - return nil, err - } - return listerat([]os.FileInfo{file}), nil - case "Readlink": - file, err := fs.fetch(r.Filepath) - if err != nil { - return nil, err - } - if file.symlink != "" { - file, err = fs.fetch(file.symlink) - if err != nil { - return nil, err - } - } - return listerat([]os.FileInfo{file}), nil - } - return nil, nil -} - -// In memory file-system-y thing that the Hanlders live on -type root struct { - *memFile - files map[string]*memFile - filesLock sync.Mutex - mockErr error -} - -// Set a mocked error that the next handler call will return. -// Set to nil to reset for no error. -func (fs *root) returnErr(err error) { - fs.mockErr = err -} - -func (fs *root) fetch(path string) (*memFile, error) { - if path == "/" { - return fs.memFile, nil - } - if file, ok := fs.files[path]; ok { - return file, nil - } - return nil, os.ErrNotExist -} - -// Implements os.FileInfo, Reader and Writer interfaces. -// These are the 3 interfaces necessary for the Handlers. -type memFile struct { - name string - modtime time.Time - symlink string - isdir bool - content []byte - contentLock sync.RWMutex -} - -// factory to make sure modtime is set -func newMemFile(name string, isdir bool) *memFile { - return &memFile{ - name: name, - modtime: time.Now(), - isdir: isdir, - } -} - -// Have memFile fulfill os.FileInfo interface -func (f *memFile) Name() string { return filepath.Base(f.name) } -func (f *memFile) Size() int64 { return int64(len(f.content)) } -func (f *memFile) Mode() os.FileMode { - ret := os.FileMode(0644) - if f.isdir { - ret = os.FileMode(0755) | os.ModeDir - } - if f.symlink != "" { - ret = os.FileMode(0777) | os.ModeSymlink - } - return ret -} -func (f *memFile) ModTime() time.Time { return f.modtime } -func (f *memFile) IsDir() bool { return f.isdir } -func (f *memFile) Sys() interface{} { - return fakeFileInfoSys() -} - -// Read/Write -func (f *memFile) ReaderAt() (io.ReaderAt, error) { - if f.isdir { - return nil, os.ErrInvalid - } - return bytes.NewReader(f.content), nil -} - -func (f *memFile) WriterAt() (io.WriterAt, error) { - if f.isdir { - return nil, os.ErrInvalid - } - return f, nil -} -func (f *memFile) WriteAt(p []byte, off int64) (int, error) { - // fmt.Println(string(p), off) - // mimic write delays, should be optional - time.Sleep(time.Microsecond * time.Duration(len(p))) - f.contentLock.Lock() - defer f.contentLock.Unlock() - plen := len(p) + int(off) - if plen >= len(f.content) { - nc := make([]byte, plen) - copy(nc, f.content) - f.content = nc - } - copy(f.content[off:], p) - return len(p), nil -} diff --git a/vendor/github.com/pkg/sftp/request-interfaces.go b/vendor/github.com/pkg/sftp/request-interfaces.go deleted file mode 100644 index f69cedc5c8b..00000000000 --- a/vendor/github.com/pkg/sftp/request-interfaces.go +++ /dev/null @@ -1,55 +0,0 @@ -package sftp - -import ( - "io" - "os" -) - -// Interfaces are differentiated based on required returned values. -// All input arguments are to be pulled from Request (the only arg). - -// The Handler interfaces all take the Request object as its only argument. -// All the data you should need to handle the call are in the Request object. -// The request.Method attribute is initially the most important one as it -// determines which Handler gets called. - -// FileReader should return an io.ReaderAt for the filepath -// Note in cases of an error, the error text will be sent to the client. -// Called for Methods: Get -type FileReader interface { - Fileread(*Request) (io.ReaderAt, error) -} - -// FileWriter should return an io.WriterAt for the filepath. -// -// The request server code will call Close() on the returned io.WriterAt -// ojbect if an io.Closer type assertion succeeds. -// Note in cases of an error, the error text will be sent to the client. -// Called for Methods: Put, Open -type FileWriter interface { - Filewrite(*Request) (io.WriterAt, error) -} - -// FileCmder should return an error -// Note in cases of an error, the error text will be sent to the client. -// Called for Methods: Setstat, Rename, Rmdir, Mkdir, Symlink, Remove -type FileCmder interface { - Filecmd(*Request) error -} - -// FileLister should return an object that fulfils the ListerAt interface -// Note in cases of an error, the error text will be sent to the client. -// Called for Methods: List, Stat, Readlink -type FileLister interface { - Filelist(*Request) (ListerAt, error) -} - -// ListerAt does for file lists what io.ReaderAt does for files. -// ListAt should return the number of entries copied and an io.EOF -// error if at end of list. This is testable by comparing how many you -// copied to how many could be copied (eg. n < len(ls) below). -// The copy() builtin is best for the copying. -// Note in cases of an error, the error text will be sent to the client. -type ListerAt interface { - ListAt([]os.FileInfo, int64) (int, error) -} diff --git a/vendor/github.com/pkg/sftp/request-readme.md b/vendor/github.com/pkg/sftp/request-readme.md deleted file mode 100644 index f887274dcca..00000000000 --- a/vendor/github.com/pkg/sftp/request-readme.md +++ /dev/null @@ -1,53 +0,0 @@ -# Request Based SFTP API - -The request based API allows for custom backends in a way similar to the http -package. In order to create a backend you need to implement 4 handler -interfaces; one for reading, one for writing, one for misc commands and one for -listing files. Each has 1 required method and in each case those methods take -the Request as the only parameter and they each return something different. -These 4 interfaces are enough to handle all the SFTP traffic in a simplified -manner. - -The Request structure has 5 public fields which you will deal with. - -- Method (string) - string name of incoming call -- Filepath (string) - POSIX path of file to act on -- Flags (uint32) - 32bit bitmask value of file open/create flags -- Attrs ([]byte) - byte string of file attribute data -- Target (string) - target path for renames and sym-links - -Below are the methods and a brief description of what they need to do. - -### Fileread(*Request) (io.Reader, error) - -Handler for "Get" method and returns an io.Reader for the file which the server -then sends to the client. - -### Filewrite(*Request) (io.Writer, error) - -Handler for "Put" method and returns an io.Writer for the file which the server -then writes the uploaded file to. The file opening "pflags" are currently -preserved in the Request.Flags field as a 32bit bitmask value. See the [SFTP -spec](https://tools.ietf.org/html/draft-ietf-secsh-filexfer-02#section-6.3) for -details. - -### Filecmd(*Request) error - -Handles "SetStat", "Rename", "Rmdir", "Mkdir" and "Symlink" methods. Makes the -appropriate changes and returns nil for success or an filesystem like error -(eg. os.ErrNotExist). The attributes are currently propagated in their raw form -([]byte) and will need to be unmarshalled to be useful. See the respond method -on sshFxpSetstatPacket for example of you might want to do this. - -### Fileinfo(*Request) ([]os.FileInfo, error) - -Handles "List", "Stat", "Readlink" methods. Gathers/creates FileInfo structs -with the data on the files and returns in a list (list of 1 for Stat and -Readlink). - - -## TODO - -- Add support for API users to see trace/debugging info of what is going on -inside SFTP server. -- Unmarshal the file attributes into a structure on the Request object. diff --git a/vendor/github.com/pkg/sftp/request-server.go b/vendor/github.com/pkg/sftp/request-server.go deleted file mode 100644 index 74ba46c8bf3..00000000000 --- a/vendor/github.com/pkg/sftp/request-server.go +++ /dev/null @@ -1,243 +0,0 @@ -package sftp - -import ( - "context" - "io" - "os" - "path" - "path/filepath" - "strconv" - "sync" - "syscall" - - "github.com/pkg/errors" -) - -var maxTxPacket uint32 = 1 << 15 - -// Handlers contains the 4 SFTP server request handlers. -type Handlers struct { - FileGet FileReader - FilePut FileWriter - FileCmd FileCmder - FileList FileLister -} - -// RequestServer abstracts the sftp protocol with an http request-like protocol -type RequestServer struct { - *serverConn - Handlers Handlers - pktMgr *packetManager - openRequests map[string]*Request - openRequestLock sync.RWMutex - handleCount int -} - -// NewRequestServer creates/allocates/returns new RequestServer. -// Normally there there will be one server per user-session. -func NewRequestServer(rwc io.ReadWriteCloser, h Handlers) *RequestServer { - svrConn := &serverConn{ - conn: conn{ - Reader: rwc, - WriteCloser: rwc, - }, - } - return &RequestServer{ - serverConn: svrConn, - Handlers: h, - pktMgr: newPktMgr(svrConn), - openRequests: make(map[string]*Request), - } -} - -// New Open packet/Request -func (rs *RequestServer) nextRequest(r *Request) string { - rs.openRequestLock.Lock() - defer rs.openRequestLock.Unlock() - rs.handleCount++ - handle := strconv.Itoa(rs.handleCount) - rs.openRequests[handle] = r - return handle -} - -// Returns Request from openRequests, bool is false if it is missing -// If the method is different, save/return a new Request w/ that Method. -// -// The Requests in openRequests work essentially as open file descriptors that -// you can do different things with. What you are doing with it are denoted by -// the first packet of that type (read/write/etc). We create a new Request when -// it changes to set the request.Method attribute in a thread safe way. -func (rs *RequestServer) getRequest(handle, method string) (*Request, bool) { - rs.openRequestLock.RLock() - r, ok := rs.openRequests[handle] - rs.openRequestLock.RUnlock() - if !ok || r.Method == method { - return r, ok - } - // if we make it here we need to replace the request - rs.openRequestLock.Lock() - defer rs.openRequestLock.Unlock() - r, ok = rs.openRequests[handle] - if !ok || r.Method == method { // re-check needed b/c lock race - return r, ok - } - r = r.copy() - r.Method = method - rs.openRequests[handle] = r - return r, ok -} - -func (rs *RequestServer) closeRequest(handle string) error { - rs.openRequestLock.Lock() - defer rs.openRequestLock.Unlock() - if r, ok := rs.openRequests[handle]; ok { - delete(rs.openRequests, handle) - return r.close() - } - return syscall.EBADF -} - -// Close the read/write/closer to trigger exiting the main server loop -func (rs *RequestServer) Close() error { return rs.conn.Close() } - -// Serve requests for user session -func (rs *RequestServer) Serve() error { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - var wg sync.WaitGroup - runWorker := func(ch chan orderedRequest) { - wg.Add(1) - go func() { - defer wg.Done() - if err := rs.packetWorker(ctx, ch); err != nil { - rs.conn.Close() // shuts down recvPacket - } - }() - } - pktChan := rs.pktMgr.workerChan(runWorker) - - var err error - var pkt requestPacket - var pktType uint8 - var pktBytes []byte - for { - pktType, pktBytes, err = rs.recvPacket() - if err != nil { - break - } - - pkt, err = makePacket(rxPacket{fxp(pktType), pktBytes}) - if err != nil { - switch errors.Cause(err) { - case errUnknownExtendedPacket: - if err := rs.serverConn.sendError(pkt, ErrSshFxOpUnsupported); err != nil { - debug("failed to send err packet: %v", err) - rs.conn.Close() // shuts down recvPacket - break - } - default: - debug("makePacket err: %v", err) - rs.conn.Close() // shuts down recvPacket - break - } - } - - pktChan <- rs.pktMgr.newOrderedRequest(pkt) - } - - close(pktChan) // shuts down sftpServerWorkers - wg.Wait() // wait for all workers to exit - - // make sure all open requests are properly closed - // (eg. possible on dropped connections, client crashes, etc.) - for handle, req := range rs.openRequests { - delete(rs.openRequests, handle) - req.close() - } - - return err -} - -func (rs *RequestServer) packetWorker( - ctx context.Context, pktChan chan orderedRequest, -) error { - for pkt := range pktChan { - var rpkt responsePacket - switch pkt := pkt.requestPacket.(type) { - case *sshFxInitPacket: - rpkt = sshFxVersionPacket{Version: sftpProtocolVersion} - case *sshFxpClosePacket: - handle := pkt.getHandle() - rpkt = statusFromError(pkt, rs.closeRequest(handle)) - case *sshFxpRealpathPacket: - rpkt = cleanPacketPath(pkt) - case *sshFxpOpendirPacket: - request := requestFromPacket(ctx, pkt) - rpkt = request.call(rs.Handlers, pkt) - if stat, ok := rpkt.(*sshFxpStatResponse); ok { - if stat.info.IsDir() { - handle := rs.nextRequest(request) - rpkt = sshFxpHandlePacket{ID: pkt.id(), Handle: handle} - } else { - rpkt = statusFromError(pkt, &os.PathError{ - Path: request.Filepath, Err: syscall.ENOTDIR}) - } - } - case *sshFxpOpenPacket: - request := requestFromPacket(ctx, pkt) - handle := rs.nextRequest(request) - rpkt = sshFxpHandlePacket{ID: pkt.id(), Handle: handle} - if pkt.hasPflags(ssh_FXF_CREAT) { - if p := request.call(rs.Handlers, pkt); !statusOk(p) { - rpkt = p // if error in write, return it - } - } - case hasHandle: - handle := pkt.getHandle() - request, ok := rs.getRequest(handle, requestMethod(pkt)) - if !ok { - rpkt = statusFromError(pkt, syscall.EBADF) - } else { - rpkt = request.call(rs.Handlers, pkt) - } - case hasPath: - request := requestFromPacket(ctx, pkt) - rpkt = request.call(rs.Handlers, pkt) - request.close() - default: - return errors.Errorf("unexpected packet type %T", pkt) - } - - rs.pktMgr.readyPacket( - rs.pktMgr.newOrderedResponse(rpkt, pkt.orderId())) - } - return nil -} - -// True is responsePacket is an OK status packet -func statusOk(rpkt responsePacket) bool { - p, ok := rpkt.(sshFxpStatusPacket) - return ok && p.StatusError.Code == ssh_FX_OK -} - -// clean and return name packet for file -func cleanPacketPath(pkt *sshFxpRealpathPacket) responsePacket { - path := cleanPath(pkt.getPath()) - return &sshFxpNamePacket{ - ID: pkt.id(), - NameAttrs: []sshFxpNameAttr{{ - Name: path, - LongName: path, - Attrs: emptyFileStat, - }}, - } -} - -// Makes sure we have a clean POSIX (/) absolute path to work with -func cleanPath(p string) string { - p = filepath.ToSlash(p) - if !filepath.IsAbs(p) { - p = "/" + p - } - return path.Clean(p) -} diff --git a/vendor/github.com/pkg/sftp/request-unix.go b/vendor/github.com/pkg/sftp/request-unix.go deleted file mode 100644 index a71a8980aeb..00000000000 --- a/vendor/github.com/pkg/sftp/request-unix.go +++ /dev/null @@ -1,23 +0,0 @@ -// +build !windows - -package sftp - -import ( - "errors" - "syscall" -) - -func fakeFileInfoSys() interface{} { - return &syscall.Stat_t{Uid: 65534, Gid: 65534} -} - -func testOsSys(sys interface{}) error { - fstat := sys.(*FileStat) - if fstat.UID != uint32(65534) { - return errors.New("Uid failed to match.") - } - if fstat.GID != uint32(65534) { - return errors.New("Gid failed to match:") - } - return nil -} diff --git a/vendor/github.com/pkg/sftp/request.go b/vendor/github.com/pkg/sftp/request.go deleted file mode 100644 index 8188c77ebf1..00000000000 --- a/vendor/github.com/pkg/sftp/request.go +++ /dev/null @@ -1,377 +0,0 @@ -package sftp - -import ( - "context" - "io" - "os" - "path" - "path/filepath" - "sync" - "syscall" - - "github.com/pkg/errors" -) - -// MaxFilelist is the max number of files to return in a readdir batch. -var MaxFilelist int64 = 100 - -// Request contains the data and state for the incoming service request. -type Request struct { - // Get, Put, Setstat, Stat, Rename, Remove - // Rmdir, Mkdir, List, Readlink, Symlink - Method string - Filepath string - Flags uint32 - Attrs []byte // convert to sub-struct - Target string // for renames and sym-links - // reader/writer/readdir from handlers - state state - // context lasts duration of request - ctx context.Context - cancelCtx context.CancelFunc -} - -type state struct { - *sync.RWMutex - writerAt io.WriterAt - readerAt io.ReaderAt - listerAt ListerAt - lsoffset int64 -} - -// New Request initialized based on packet data -func requestFromPacket(ctx context.Context, pkt hasPath) *Request { - method := requestMethod(pkt) - request := NewRequest(method, pkt.getPath()) - request.ctx, request.cancelCtx = context.WithCancel(ctx) - - switch p := pkt.(type) { - case *sshFxpOpenPacket: - request.Flags = p.Pflags - case *sshFxpSetstatPacket: - request.Flags = p.Flags - request.Attrs = p.Attrs.([]byte) - case *sshFxpRenamePacket: - request.Target = cleanPath(p.Newpath) - case *sshFxpSymlinkPacket: - request.Target = cleanPath(p.Linkpath) - } - return request -} - -// NewRequest creates a new Request object. -func NewRequest(method, path string) *Request { - return &Request{Method: method, Filepath: cleanPath(path), - state: state{RWMutex: new(sync.RWMutex)}} -} - -// shallow copy of existing request -func (r *Request) copy() *Request { - r.state.Lock() - defer r.state.Unlock() - r2 := new(Request) - *r2 = *r - return r2 -} - -// Context returns the request's context. To change the context, -// use WithContext. -// -// The returned context is always non-nil; it defaults to the -// background context. -// -// For incoming server requests, the context is canceled when the -// request is complete or the client's connection closes. -func (r *Request) Context() context.Context { - if r.ctx != nil { - return r.ctx - } - return context.Background() -} - -// WithContext returns a copy of r with its context changed to ctx. -// The provided ctx must be non-nil. -func (r *Request) WithContext(ctx context.Context) *Request { - if ctx == nil { - panic("nil context") - } - r2 := r.copy() - r2.ctx = ctx - r2.cancelCtx = nil - return r2 -} - -// Returns current offset for file list -func (r *Request) lsNext() int64 { - r.state.RLock() - defer r.state.RUnlock() - return r.state.lsoffset -} - -// Increases next offset -func (r *Request) lsInc(offset int64) { - r.state.Lock() - defer r.state.Unlock() - r.state.lsoffset = r.state.lsoffset + offset -} - -// manage file read/write state -func (r *Request) setListerState(la ListerAt) { - r.state.Lock() - defer r.state.Unlock() - r.state.listerAt = la -} - -func (r *Request) getLister() ListerAt { - r.state.RLock() - defer r.state.RUnlock() - return r.state.listerAt -} - -// Close reader/writer if possible -func (r *Request) close() error { - defer func() { - if r.cancelCtx != nil { - r.cancelCtx() - } - }() - r.state.RLock() - rd := r.state.readerAt - r.state.RUnlock() - if c, ok := rd.(io.Closer); ok { - return c.Close() - } - r.state.RLock() - wt := r.state.writerAt - r.state.RUnlock() - if c, ok := wt.(io.Closer); ok { - return c.Close() - } - return nil -} - -// called from worker to handle packet/request -func (r *Request) call(handlers Handlers, pkt requestPacket) responsePacket { - switch r.Method { - case "Get": - return fileget(handlers.FileGet, r, pkt) - case "Put", "Open": - return fileput(handlers.FilePut, r, pkt) - case "Setstat", "Rename", "Rmdir", "Mkdir", "Symlink", "Remove": - return filecmd(handlers.FileCmd, r, pkt) - case "List": - return filelist(handlers.FileList, r, pkt) - case "Stat", "Readlink": - return filestat(handlers.FileList, r, pkt) - default: - return statusFromError(pkt, - errors.Errorf("unexpected method: %s", r.Method)) - } -} - -// file data for additional read/write packets -func packetData(p requestPacket) (data []byte, offset int64, length uint32) { - switch p := p.(type) { - case *sshFxpReadPacket: - length = p.Len - offset = int64(p.Offset) - case *sshFxpWritePacket: - data = p.Data - length = p.Length - offset = int64(p.Offset) - } - return -} - -// wrap FileReader handler -func fileget(h FileReader, r *Request, pkt requestPacket) responsePacket { - var err error - r.state.RLock() - reader := r.state.readerAt - r.state.RUnlock() - if reader == nil { - r.state.Lock() - if r.state.readerAt == nil { - r.state.readerAt, err = h.Fileread(r) - if err != nil { - r.state.Unlock() - return statusFromError(pkt, err) - } - } - reader = r.state.readerAt - r.state.Unlock() - } - - _, offset, length := packetData(pkt) - data := make([]byte, clamp(length, maxTxPacket)) - n, err := reader.ReadAt(data, offset) - // only return EOF erro if no data left to read - if err != nil && (err != io.EOF || n == 0) { - return statusFromError(pkt, err) - } - return &sshFxpDataPacket{ - ID: pkt.id(), - Length: uint32(n), - Data: data[:n], - } -} - -// wrap FileWriter handler -func fileput(h FileWriter, r *Request, pkt requestPacket) responsePacket { - var err error - r.state.RLock() - writer := r.state.writerAt - r.state.RUnlock() - if writer == nil { - r.state.Lock() - if r.state.writerAt == nil { - r.state.writerAt, err = h.Filewrite(r) - if err != nil { - r.state.Unlock() - return statusFromError(pkt, err) - } - } - writer = r.state.writerAt - r.state.Unlock() - } - - data, offset, _ := packetData(pkt) - _, err = writer.WriteAt(data, offset) - return statusFromError(pkt, err) -} - -// wrap FileCmder handler -func filecmd(h FileCmder, r *Request, pkt requestPacket) responsePacket { - - switch p := pkt.(type) { - case *sshFxpFsetstatPacket: - r.Flags = p.Flags - r.Attrs = p.Attrs.([]byte) - } - err := h.Filecmd(r) - return statusFromError(pkt, err) -} - -// wrap FileLister handler -func filelist(h FileLister, r *Request, pkt requestPacket) responsePacket { - var err error - lister := r.getLister() - if lister == nil { - lister, err = h.Filelist(r) - if err != nil { - return statusFromError(pkt, err) - } - r.setListerState(lister) - } - - offset := r.lsNext() - finfo := make([]os.FileInfo, MaxFilelist) - n, err := lister.ListAt(finfo, offset) - r.lsInc(int64(n)) - // ignore EOF as we only return it when there are no results - finfo = finfo[:n] // avoid need for nil tests below - - switch r.Method { - case "List": - if err != nil && err != io.EOF { - return statusFromError(pkt, err) - } - if err == io.EOF && n == 0 { - return statusFromError(pkt, io.EOF) - } - dirname := filepath.ToSlash(path.Base(r.Filepath)) - ret := &sshFxpNamePacket{ID: pkt.id()} - - for _, fi := range finfo { - ret.NameAttrs = append(ret.NameAttrs, sshFxpNameAttr{ - Name: fi.Name(), - LongName: runLs(dirname, fi), - Attrs: []interface{}{fi}, - }) - } - return ret - default: - err = errors.Errorf("unexpected method: %s", r.Method) - return statusFromError(pkt, err) - } -} - -func filestat(h FileLister, r *Request, pkt requestPacket) responsePacket { - lister, err := h.Filelist(r) - if err != nil { - return statusFromError(pkt, err) - } - finfo := make([]os.FileInfo, 1) - n, err := lister.ListAt(finfo, 0) - finfo = finfo[:n] // avoid need for nil tests below - - switch r.Method { - case "Stat": - if err != nil && err != io.EOF { - return statusFromError(pkt, err) - } - if n == 0 { - err = &os.PathError{Op: "stat", Path: r.Filepath, - Err: syscall.ENOENT} - return statusFromError(pkt, err) - } - return &sshFxpStatResponse{ - ID: pkt.id(), - info: finfo[0], - } - case "Readlink": - if err != nil && err != io.EOF { - return statusFromError(pkt, err) - } - if n == 0 { - err = &os.PathError{Op: "readlink", Path: r.Filepath, - Err: syscall.ENOENT} - return statusFromError(pkt, err) - } - filename := finfo[0].Name() - return &sshFxpNamePacket{ - ID: pkt.id(), - NameAttrs: []sshFxpNameAttr{{ - Name: filename, - LongName: filename, - Attrs: emptyFileStat, - }}, - } - default: - err = errors.Errorf("unexpected method: %s", r.Method) - return statusFromError(pkt, err) - } -} - -// init attributes of request object from packet data -func requestMethod(p requestPacket) (method string) { - switch p.(type) { - case *sshFxpReadPacket: - method = "Get" - case *sshFxpWritePacket: - method = "Put" - case *sshFxpReaddirPacket: - method = "List" - case *sshFxpOpenPacket: - method = "Open" - case *sshFxpOpendirPacket: - method = "Stat" - case *sshFxpSetstatPacket, *sshFxpFsetstatPacket: - method = "Setstat" - case *sshFxpRenamePacket: - method = "Rename" - case *sshFxpSymlinkPacket: - method = "Symlink" - case *sshFxpRemovePacket: - method = "Remove" - case *sshFxpStatPacket, *sshFxpLstatPacket, *sshFxpFstatPacket: - method = "Stat" - case *sshFxpRmdirPacket: - method = "Rmdir" - case *sshFxpReadlinkPacket: - method = "Readlink" - case *sshFxpMkdirPacket: - method = "Mkdir" - } - return method -} diff --git a/vendor/github.com/pkg/sftp/request_windows.go b/vendor/github.com/pkg/sftp/request_windows.go deleted file mode 100644 index 94d306b6e98..00000000000 --- a/vendor/github.com/pkg/sftp/request_windows.go +++ /dev/null @@ -1,11 +0,0 @@ -package sftp - -import "syscall" - -func fakeFileInfoSys() interface{} { - return syscall.Win32FileAttributeData{} -} - -func testOsSys(sys interface{}) error { - return nil -} diff --git a/vendor/github.com/pkg/sftp/server.go b/vendor/github.com/pkg/sftp/server.go deleted file mode 100644 index 0fac1b66843..00000000000 --- a/vendor/github.com/pkg/sftp/server.go +++ /dev/null @@ -1,679 +0,0 @@ -package sftp - -// sftp server counterpart - -import ( - "encoding" - "fmt" - "io" - "io/ioutil" - "os" - "path/filepath" - "strconv" - "sync" - "syscall" - "time" - - "github.com/pkg/errors" -) - -const ( - SftpServerWorkerCount = 8 -) - -// Server is an SSH File Transfer Protocol (sftp) server. -// This is intended to provide the sftp subsystem to an ssh server daemon. -// This implementation currently supports most of sftp server protocol version 3, -// as specified at http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02 -type Server struct { - *serverConn - debugStream io.Writer - readOnly bool - pktMgr *packetManager - openFiles map[string]*os.File - openFilesLock sync.RWMutex - handleCount int - maxTxPacket uint32 -} - -func (svr *Server) nextHandle(f *os.File) string { - svr.openFilesLock.Lock() - defer svr.openFilesLock.Unlock() - svr.handleCount++ - handle := strconv.Itoa(svr.handleCount) - svr.openFiles[handle] = f - return handle -} - -func (svr *Server) closeHandle(handle string) error { - svr.openFilesLock.Lock() - defer svr.openFilesLock.Unlock() - if f, ok := svr.openFiles[handle]; ok { - delete(svr.openFiles, handle) - return f.Close() - } - - return syscall.EBADF -} - -func (svr *Server) getHandle(handle string) (*os.File, bool) { - svr.openFilesLock.RLock() - defer svr.openFilesLock.RUnlock() - f, ok := svr.openFiles[handle] - return f, ok -} - -type serverRespondablePacket interface { - encoding.BinaryUnmarshaler - id() uint32 - respond(svr *Server) responsePacket -} - -// NewServer creates a new Server instance around the provided streams, serving -// content from the root of the filesystem. Optionally, ServerOption -// functions may be specified to further configure the Server. -// -// A subsequent call to Serve() is required to begin serving files over SFTP. -func NewServer(rwc io.ReadWriteCloser, options ...ServerOption) (*Server, error) { - svrConn := &serverConn{ - conn: conn{ - Reader: rwc, - WriteCloser: rwc, - }, - } - s := &Server{ - serverConn: svrConn, - debugStream: ioutil.Discard, - pktMgr: newPktMgr(svrConn), - openFiles: make(map[string]*os.File), - maxTxPacket: 1 << 15, - } - - for _, o := range options { - if err := o(s); err != nil { - return nil, err - } - } - - return s, nil -} - -// A ServerOption is a function which applies configuration to a Server. -type ServerOption func(*Server) error - -// WithDebug enables Server debugging output to the supplied io.Writer. -func WithDebug(w io.Writer) ServerOption { - return func(s *Server) error { - s.debugStream = w - return nil - } -} - -// ReadOnly configures a Server to serve files in read-only mode. -func ReadOnly() ServerOption { - return func(s *Server) error { - s.readOnly = true - return nil - } -} - -type rxPacket struct { - pktType fxp - pktBytes []byte -} - -// Up to N parallel servers -func (svr *Server) sftpServerWorker(pktChan chan orderedRequest) error { - for pkt := range pktChan { - // readonly checks - readonly := true - switch pkt := pkt.requestPacket.(type) { - case notReadOnly: - readonly = false - case *sshFxpOpenPacket: - readonly = pkt.readonly() - case *sshFxpExtendedPacket: - readonly = pkt.readonly() - } - - // If server is operating read-only and a write operation is requested, - // return permission denied - if !readonly && svr.readOnly { - svr.sendPacket(orderedResponse{ - responsePacket: statusFromError(pkt, syscall.EPERM), - orderid: pkt.orderId()}) - continue - } - - if err := handlePacket(svr, pkt); err != nil { - return err - } - } - return nil -} - -func handlePacket(s *Server, p orderedRequest) error { - var rpkt responsePacket - switch p := p.requestPacket.(type) { - case *sshFxInitPacket: - rpkt = sshFxVersionPacket{Version: sftpProtocolVersion} - case *sshFxpStatPacket: - // stat the requested file - info, err := os.Stat(p.Path) - rpkt = sshFxpStatResponse{ - ID: p.ID, - info: info, - } - if err != nil { - rpkt = statusFromError(p, err) - } - case *sshFxpLstatPacket: - // stat the requested file - info, err := os.Lstat(p.Path) - rpkt = sshFxpStatResponse{ - ID: p.ID, - info: info, - } - if err != nil { - rpkt = statusFromError(p, err) - } - case *sshFxpFstatPacket: - f, ok := s.getHandle(p.Handle) - var err error = syscall.EBADF - var info os.FileInfo - if ok { - info, err = f.Stat() - rpkt = sshFxpStatResponse{ - ID: p.ID, - info: info, - } - } - if err != nil { - rpkt = statusFromError(p, err) - } - case *sshFxpMkdirPacket: - // TODO FIXME: ignore flags field - err := os.Mkdir(p.Path, 0755) - rpkt = statusFromError(p, err) - case *sshFxpRmdirPacket: - err := os.Remove(p.Path) - rpkt = statusFromError(p, err) - case *sshFxpRemovePacket: - err := os.Remove(p.Filename) - rpkt = statusFromError(p, err) - case *sshFxpRenamePacket: - err := os.Rename(p.Oldpath, p.Newpath) - rpkt = statusFromError(p, err) - case *sshFxpSymlinkPacket: - err := os.Symlink(p.Targetpath, p.Linkpath) - rpkt = statusFromError(p, err) - case *sshFxpClosePacket: - rpkt = statusFromError(p, s.closeHandle(p.Handle)) - case *sshFxpReadlinkPacket: - f, err := os.Readlink(p.Path) - rpkt = sshFxpNamePacket{ - ID: p.ID, - NameAttrs: []sshFxpNameAttr{{ - Name: f, - LongName: f, - Attrs: emptyFileStat, - }}, - } - if err != nil { - rpkt = statusFromError(p, err) - } - case *sshFxpRealpathPacket: - f, err := filepath.Abs(p.Path) - f = cleanPath(f) - rpkt = sshFxpNamePacket{ - ID: p.ID, - NameAttrs: []sshFxpNameAttr{{ - Name: f, - LongName: f, - Attrs: emptyFileStat, - }}, - } - if err != nil { - rpkt = statusFromError(p, err) - } - case *sshFxpOpendirPacket: - if stat, err := os.Stat(p.Path); err != nil { - rpkt = statusFromError(p, err) - } else if !stat.IsDir() { - rpkt = statusFromError(p, &os.PathError{ - Path: p.Path, Err: syscall.ENOTDIR}) - } else { - rpkt = sshFxpOpenPacket{ - ID: p.ID, - Path: p.Path, - Pflags: ssh_FXF_READ, - }.respond(s) - } - case *sshFxpReadPacket: - var err error = syscall.EBADF - f, ok := s.getHandle(p.Handle) - if ok { - err = nil - data := make([]byte, clamp(p.Len, s.maxTxPacket)) - n, _err := f.ReadAt(data, int64(p.Offset)) - if _err != nil && (_err != io.EOF || n == 0) { - err = _err - } - rpkt = sshFxpDataPacket{ - ID: p.ID, - Length: uint32(n), - Data: data[:n], - } - } - if err != nil { - rpkt = statusFromError(p, err) - } - - case *sshFxpWritePacket: - f, ok := s.getHandle(p.Handle) - var err error = syscall.EBADF - if ok { - _, err = f.WriteAt(p.Data, int64(p.Offset)) - } - rpkt = statusFromError(p, err) - case serverRespondablePacket: - rpkt = p.respond(s) - default: - return errors.Errorf("unexpected packet type %T", p) - } - - s.pktMgr.readyPacket(s.pktMgr.newOrderedResponse(rpkt, p.orderId())) - return nil -} - -// Serve serves SFTP connections until the streams stop or the SFTP subsystem -// is stopped. -func (svr *Server) Serve() error { - var wg sync.WaitGroup - runWorker := func(ch chan orderedRequest) { - wg.Add(1) - go func() { - defer wg.Done() - if err := svr.sftpServerWorker(ch); err != nil { - svr.conn.Close() // shuts down recvPacket - } - }() - } - pktChan := svr.pktMgr.workerChan(runWorker) - - var err error - var pkt requestPacket - var pktType uint8 - var pktBytes []byte - for { - pktType, pktBytes, err = svr.recvPacket() - if err != nil { - break - } - - pkt, err = makePacket(rxPacket{fxp(pktType), pktBytes}) - if err != nil { - switch errors.Cause(err) { - case errUnknownExtendedPacket: - if err := svr.serverConn.sendError(pkt, ErrSshFxOpUnsupported); err != nil { - debug("failed to send err packet: %v", err) - svr.conn.Close() // shuts down recvPacket - break - } - default: - debug("makePacket err: %v", err) - svr.conn.Close() // shuts down recvPacket - break - } - } - - pktChan <- svr.pktMgr.newOrderedRequest(pkt) - } - - close(pktChan) // shuts down sftpServerWorkers - wg.Wait() // wait for all workers to exit - - // close any still-open files - for handle, file := range svr.openFiles { - fmt.Fprintf(svr.debugStream, "sftp server file with handle %q left open: %v\n", handle, file.Name()) - file.Close() - } - return err // error from recvPacket -} - -type ider interface { - id() uint32 -} - -// The init packet has no ID, so we just return a zero-value ID -func (p sshFxInitPacket) id() uint32 { return 0 } - -type sshFxpStatResponse struct { - ID uint32 - info os.FileInfo -} - -func (p sshFxpStatResponse) MarshalBinary() ([]byte, error) { - b := []byte{ssh_FXP_ATTRS} - b = marshalUint32(b, p.ID) - b = marshalFileInfo(b, p.info) - return b, nil -} - -var emptyFileStat = []interface{}{uint32(0)} - -func (p sshFxpOpenPacket) readonly() bool { - return !p.hasPflags(ssh_FXF_WRITE) -} - -func (p sshFxpOpenPacket) hasPflags(flags ...uint32) bool { - for _, f := range flags { - if p.Pflags&f == 0 { - return false - } - } - return true -} - -func (p sshFxpOpenPacket) respond(svr *Server) responsePacket { - var osFlags int - if p.hasPflags(ssh_FXF_READ, ssh_FXF_WRITE) { - osFlags |= os.O_RDWR - } else if p.hasPflags(ssh_FXF_WRITE) { - osFlags |= os.O_WRONLY - } else if p.hasPflags(ssh_FXF_READ) { - osFlags |= os.O_RDONLY - } else { - // how are they opening? - return statusFromError(p, syscall.EINVAL) - } - - if p.hasPflags(ssh_FXF_APPEND) { - osFlags |= os.O_APPEND - } - if p.hasPflags(ssh_FXF_CREAT) { - osFlags |= os.O_CREATE - } - if p.hasPflags(ssh_FXF_TRUNC) { - osFlags |= os.O_TRUNC - } - if p.hasPflags(ssh_FXF_EXCL) { - osFlags |= os.O_EXCL - } - - f, err := os.OpenFile(p.Path, osFlags, 0644) - if err != nil { - return statusFromError(p, err) - } - - handle := svr.nextHandle(f) - return sshFxpHandlePacket{ID: p.id(), Handle: handle} -} - -func (p sshFxpReaddirPacket) respond(svr *Server) responsePacket { - f, ok := svr.getHandle(p.Handle) - if !ok { - return statusFromError(p, syscall.EBADF) - } - - dirname := f.Name() - dirents, err := f.Readdir(128) - if err != nil { - return statusFromError(p, err) - } - - ret := sshFxpNamePacket{ID: p.ID} - for _, dirent := range dirents { - ret.NameAttrs = append(ret.NameAttrs, sshFxpNameAttr{ - Name: dirent.Name(), - LongName: runLs(dirname, dirent), - Attrs: []interface{}{dirent}, - }) - } - return ret -} - -func (p sshFxpSetstatPacket) respond(svr *Server) responsePacket { - // additional unmarshalling is required for each possibility here - b := p.Attrs.([]byte) - var err error - - debug("setstat name \"%s\"", p.Path) - if (p.Flags & ssh_FILEXFER_ATTR_SIZE) != 0 { - var size uint64 - if size, b, err = unmarshalUint64Safe(b); err == nil { - err = os.Truncate(p.Path, int64(size)) - } - } - if (p.Flags & ssh_FILEXFER_ATTR_PERMISSIONS) != 0 { - var mode uint32 - if mode, b, err = unmarshalUint32Safe(b); err == nil { - err = os.Chmod(p.Path, os.FileMode(mode)) - } - } - if (p.Flags & ssh_FILEXFER_ATTR_ACMODTIME) != 0 { - var atime uint32 - var mtime uint32 - if atime, b, err = unmarshalUint32Safe(b); err != nil { - } else if mtime, b, err = unmarshalUint32Safe(b); err != nil { - } else { - atimeT := time.Unix(int64(atime), 0) - mtimeT := time.Unix(int64(mtime), 0) - err = os.Chtimes(p.Path, atimeT, mtimeT) - } - } - if (p.Flags & ssh_FILEXFER_ATTR_UIDGID) != 0 { - var uid uint32 - var gid uint32 - if uid, b, err = unmarshalUint32Safe(b); err != nil { - } else if gid, _, err = unmarshalUint32Safe(b); err != nil { - } else { - err = os.Chown(p.Path, int(uid), int(gid)) - } - } - - return statusFromError(p, err) -} - -func (p sshFxpFsetstatPacket) respond(svr *Server) responsePacket { - f, ok := svr.getHandle(p.Handle) - if !ok { - return statusFromError(p, syscall.EBADF) - } - - // additional unmarshalling is required for each possibility here - b := p.Attrs.([]byte) - var err error - - debug("fsetstat name \"%s\"", f.Name()) - if (p.Flags & ssh_FILEXFER_ATTR_SIZE) != 0 { - var size uint64 - if size, b, err = unmarshalUint64Safe(b); err == nil { - err = f.Truncate(int64(size)) - } - } - if (p.Flags & ssh_FILEXFER_ATTR_PERMISSIONS) != 0 { - var mode uint32 - if mode, b, err = unmarshalUint32Safe(b); err == nil { - err = f.Chmod(os.FileMode(mode)) - } - } - if (p.Flags & ssh_FILEXFER_ATTR_ACMODTIME) != 0 { - var atime uint32 - var mtime uint32 - if atime, b, err = unmarshalUint32Safe(b); err != nil { - } else if mtime, b, err = unmarshalUint32Safe(b); err != nil { - } else { - atimeT := time.Unix(int64(atime), 0) - mtimeT := time.Unix(int64(mtime), 0) - err = os.Chtimes(f.Name(), atimeT, mtimeT) - } - } - if (p.Flags & ssh_FILEXFER_ATTR_UIDGID) != 0 { - var uid uint32 - var gid uint32 - if uid, b, err = unmarshalUint32Safe(b); err != nil { - } else if gid, _, err = unmarshalUint32Safe(b); err != nil { - } else { - err = f.Chown(int(uid), int(gid)) - } - } - - return statusFromError(p, err) -} - -// translateErrno translates a syscall error number to a SFTP error code. -func translateErrno(errno syscall.Errno) uint32 { - switch errno { - case 0: - return ssh_FX_OK - case syscall.ENOENT: - return ssh_FX_NO_SUCH_FILE - case syscall.EPERM: - return ssh_FX_PERMISSION_DENIED - } - - return ssh_FX_FAILURE -} - -func statusFromError(p ider, err error) sshFxpStatusPacket { - ret := sshFxpStatusPacket{ - ID: p.id(), - StatusError: StatusError{ - // ssh_FX_OK = 0 - // ssh_FX_EOF = 1 - // ssh_FX_NO_SUCH_FILE = 2 ENOENT - // ssh_FX_PERMISSION_DENIED = 3 - // ssh_FX_FAILURE = 4 - // ssh_FX_BAD_MESSAGE = 5 - // ssh_FX_NO_CONNECTION = 6 - // ssh_FX_CONNECTION_LOST = 7 - // ssh_FX_OP_UNSUPPORTED = 8 - Code: ssh_FX_OK, - }, - } - if err == nil { - return ret - } - - debug("statusFromError: error is %T %#v", err, err) - ret.StatusError.Code = ssh_FX_FAILURE - ret.StatusError.msg = err.Error() - - switch e := err.(type) { - case syscall.Errno: - ret.StatusError.Code = translateErrno(e) - case *os.PathError: - debug("statusFromError,pathError: error is %T %#v", e.Err, e.Err) - if errno, ok := e.Err.(syscall.Errno); ok { - ret.StatusError.Code = translateErrno(errno) - } - case fxerr: - ret.StatusError.Code = uint32(e) - default: - switch e { - case io.EOF: - ret.StatusError.Code = ssh_FX_EOF - case os.ErrNotExist: - ret.StatusError.Code = ssh_FX_NO_SUCH_FILE - } - } - - return ret -} - -func clamp(v, max uint32) uint32 { - if v > max { - return max - } - return v -} - -func runLsTypeWord(dirent os.FileInfo) string { - // find first character, the type char - // b Block special file. - // c Character special file. - // d Directory. - // l Symbolic link. - // s Socket link. - // p FIFO. - // - Regular file. - tc := '-' - mode := dirent.Mode() - if (mode & os.ModeDir) != 0 { - tc = 'd' - } else if (mode & os.ModeDevice) != 0 { - tc = 'b' - if (mode & os.ModeCharDevice) != 0 { - tc = 'c' - } - } else if (mode & os.ModeSymlink) != 0 { - tc = 'l' - } else if (mode & os.ModeSocket) != 0 { - tc = 's' - } else if (mode & os.ModeNamedPipe) != 0 { - tc = 'p' - } - - // owner - orc := '-' - if (mode & 0400) != 0 { - orc = 'r' - } - owc := '-' - if (mode & 0200) != 0 { - owc = 'w' - } - oxc := '-' - ox := (mode & 0100) != 0 - setuid := (mode & os.ModeSetuid) != 0 - if ox && setuid { - oxc = 's' - } else if setuid { - oxc = 'S' - } else if ox { - oxc = 'x' - } - - // group - grc := '-' - if (mode & 040) != 0 { - grc = 'r' - } - gwc := '-' - if (mode & 020) != 0 { - gwc = 'w' - } - gxc := '-' - gx := (mode & 010) != 0 - setgid := (mode & os.ModeSetgid) != 0 - if gx && setgid { - gxc = 's' - } else if setgid { - gxc = 'S' - } else if gx { - gxc = 'x' - } - - // all / others - arc := '-' - if (mode & 04) != 0 { - arc = 'r' - } - awc := '-' - if (mode & 02) != 0 { - awc = 'w' - } - axc := '-' - ax := (mode & 01) != 0 - sticky := (mode & os.ModeSticky) != 0 - if ax && sticky { - axc = 't' - } else if sticky { - axc = 'T' - } else if ax { - axc = 'x' - } - - return fmt.Sprintf("%c%c%c%c%c%c%c%c%c%c", tc, orc, owc, oxc, grc, gwc, gxc, arc, awc, axc) -} diff --git a/vendor/github.com/pkg/sftp/server_statvfs_darwin.go b/vendor/github.com/pkg/sftp/server_statvfs_darwin.go deleted file mode 100644 index 8c01dac52d0..00000000000 --- a/vendor/github.com/pkg/sftp/server_statvfs_darwin.go +++ /dev/null @@ -1,21 +0,0 @@ -package sftp - -import ( - "syscall" -) - -func statvfsFromStatfst(stat *syscall.Statfs_t) (*StatVFS, error) { - return &StatVFS{ - Bsize: uint64(stat.Bsize), - Frsize: uint64(stat.Bsize), // fragment size is a linux thing; use block size here - Blocks: stat.Blocks, - Bfree: stat.Bfree, - Bavail: stat.Bavail, - Files: stat.Files, - Ffree: stat.Ffree, - Favail: stat.Ffree, // not sure how to calculate Favail - Fsid: uint64(uint64(stat.Fsid.Val[1])<<32 | uint64(stat.Fsid.Val[0])), // endianness? - Flag: uint64(stat.Flags), // assuming POSIX? - Namemax: 1024, // man 2 statfs shows: #define MAXPATHLEN 1024 - }, nil -} diff --git a/vendor/github.com/pkg/sftp/server_statvfs_impl.go b/vendor/github.com/pkg/sftp/server_statvfs_impl.go deleted file mode 100644 index 4cf91dc83d7..00000000000 --- a/vendor/github.com/pkg/sftp/server_statvfs_impl.go +++ /dev/null @@ -1,25 +0,0 @@ -// +build darwin linux - -// fill in statvfs structure with OS specific values -// Statfs_t is different per-kernel, and only exists on some unixes (not Solaris for instance) - -package sftp - -import ( - "syscall" -) - -func (p sshFxpExtendedPacketStatVFS) respond(svr *Server) responsePacket { - stat := &syscall.Statfs_t{} - if err := syscall.Statfs(p.Path, stat); err != nil { - return statusFromError(p, err) - } - - retPkt, err := statvfsFromStatfst(stat) - if err != nil { - return statusFromError(p, err) - } - retPkt.ID = p.ID - - return retPkt -} diff --git a/vendor/github.com/pkg/sftp/server_statvfs_linux.go b/vendor/github.com/pkg/sftp/server_statvfs_linux.go deleted file mode 100644 index 1d180d47c9a..00000000000 --- a/vendor/github.com/pkg/sftp/server_statvfs_linux.go +++ /dev/null @@ -1,22 +0,0 @@ -// +build linux - -package sftp - -import ( - "syscall" -) - -func statvfsFromStatfst(stat *syscall.Statfs_t) (*StatVFS, error) { - return &StatVFS{ - Bsize: uint64(stat.Bsize), - Frsize: uint64(stat.Frsize), - Blocks: stat.Blocks, - Bfree: stat.Bfree, - Bavail: stat.Bavail, - Files: stat.Files, - Ffree: stat.Ffree, - Favail: stat.Ffree, // not sure how to calculate Favail - Flag: uint64(stat.Flags), // assuming POSIX? - Namemax: uint64(stat.Namelen), - }, nil -} diff --git a/vendor/github.com/pkg/sftp/server_statvfs_stubs.go b/vendor/github.com/pkg/sftp/server_statvfs_stubs.go deleted file mode 100644 index c6f61643cc5..00000000000 --- a/vendor/github.com/pkg/sftp/server_statvfs_stubs.go +++ /dev/null @@ -1,11 +0,0 @@ -// +build !darwin,!linux - -package sftp - -import ( - "syscall" -) - -func (p sshFxpExtendedPacketStatVFS) respond(svr *Server) responsePacket { - return statusFromError(p, syscall.ENOTSUP) -} diff --git a/vendor/github.com/pkg/sftp/server_stubs.go b/vendor/github.com/pkg/sftp/server_stubs.go deleted file mode 100644 index a14c7348248..00000000000 --- a/vendor/github.com/pkg/sftp/server_stubs.go +++ /dev/null @@ -1,32 +0,0 @@ -// +build !cgo,!plan9 windows android - -package sftp - -import ( - "os" - "time" - "fmt" -) - -func runLs(dirname string, dirent os.FileInfo) string { - typeword := runLsTypeWord(dirent) - numLinks := 1 - if dirent.IsDir() { - numLinks = 0 - } - username := "root" - groupname := "root" - mtime := dirent.ModTime() - monthStr := mtime.Month().String()[0:3] - day := mtime.Day() - year := mtime.Year() - now := time.Now() - isOld := mtime.Before(now.Add(-time.Hour * 24 * 365 / 2)) - - yearOrTime := fmt.Sprintf("%02d:%02d", mtime.Hour(), mtime.Minute()) - if isOld { - yearOrTime = fmt.Sprintf("%d", year) - } - - return fmt.Sprintf("%s %4d %-8s %-8s %8d %s %2d %5s %s", typeword, numLinks, username, groupname, dirent.Size(), monthStr, day, yearOrTime, dirent.Name()) -} diff --git a/vendor/github.com/pkg/sftp/server_unix.go b/vendor/github.com/pkg/sftp/server_unix.go deleted file mode 100644 index abceca498f9..00000000000 --- a/vendor/github.com/pkg/sftp/server_unix.go +++ /dev/null @@ -1,54 +0,0 @@ -// +build darwin dragonfly freebsd !android,linux netbsd openbsd solaris aix -// +build cgo - -package sftp - -import ( - "fmt" - "os" - "path" - "syscall" - "time" -) - -func runLsStatt(dirent os.FileInfo, statt *syscall.Stat_t) string { - // example from openssh sftp server: - // crw-rw-rw- 1 root wheel 0 Jul 31 20:52 ttyvd - // format: - // {directory / char device / etc}{rwxrwxrwx} {number of links} owner group size month day [time (this year) | year (otherwise)] name - - typeword := runLsTypeWord(dirent) - numLinks := statt.Nlink - uid := statt.Uid - gid := statt.Gid - username := fmt.Sprintf("%d", uid) - groupname := fmt.Sprintf("%d", gid) - // TODO FIXME: uid -> username, gid -> groupname lookup for ls -l format output - - mtime := dirent.ModTime() - monthStr := mtime.Month().String()[0:3] - day := mtime.Day() - year := mtime.Year() - now := time.Now() - isOld := mtime.Before(now.Add(-time.Hour * 24 * 365 / 2)) - - yearOrTime := fmt.Sprintf("%02d:%02d", mtime.Hour(), mtime.Minute()) - if isOld { - yearOrTime = fmt.Sprintf("%d", year) - } - - return fmt.Sprintf("%s %4d %-8s %-8s %8d %s %2d %5s %s", typeword, numLinks, username, groupname, dirent.Size(), monthStr, day, yearOrTime, dirent.Name()) -} - -// ls -l style output for a file, which is in the 'long output' section of a readdir response packet -// this is a very simple (lazy) implementation, just enough to look almost like openssh in a few basic cases -func runLs(dirname string, dirent os.FileInfo) string { - dsys := dirent.Sys() - if dsys == nil { - } else if statt, ok := dsys.(*syscall.Stat_t); !ok { - } else { - return runLsStatt(dirent, statt) - } - - return path.Join(dirname, dirent.Name()) -} diff --git a/vendor/github.com/pkg/sftp/sftp.go b/vendor/github.com/pkg/sftp/sftp.go deleted file mode 100644 index 3cdb14df82b..00000000000 --- a/vendor/github.com/pkg/sftp/sftp.go +++ /dev/null @@ -1,217 +0,0 @@ -// Package sftp implements the SSH File Transfer Protocol as described in -// https://tools.ietf.org/html/draft-ietf-secsh-filexfer-02 -package sftp - -import ( - "fmt" - - "github.com/pkg/errors" -) - -const ( - ssh_FXP_INIT = 1 - ssh_FXP_VERSION = 2 - ssh_FXP_OPEN = 3 - ssh_FXP_CLOSE = 4 - ssh_FXP_READ = 5 - ssh_FXP_WRITE = 6 - ssh_FXP_LSTAT = 7 - ssh_FXP_FSTAT = 8 - ssh_FXP_SETSTAT = 9 - ssh_FXP_FSETSTAT = 10 - ssh_FXP_OPENDIR = 11 - ssh_FXP_READDIR = 12 - ssh_FXP_REMOVE = 13 - ssh_FXP_MKDIR = 14 - ssh_FXP_RMDIR = 15 - ssh_FXP_REALPATH = 16 - ssh_FXP_STAT = 17 - ssh_FXP_RENAME = 18 - ssh_FXP_READLINK = 19 - ssh_FXP_SYMLINK = 20 - ssh_FXP_STATUS = 101 - ssh_FXP_HANDLE = 102 - ssh_FXP_DATA = 103 - ssh_FXP_NAME = 104 - ssh_FXP_ATTRS = 105 - ssh_FXP_EXTENDED = 200 - ssh_FXP_EXTENDED_REPLY = 201 -) - -const ( - ssh_FX_OK = 0 - ssh_FX_EOF = 1 - ssh_FX_NO_SUCH_FILE = 2 - ssh_FX_PERMISSION_DENIED = 3 - ssh_FX_FAILURE = 4 - ssh_FX_BAD_MESSAGE = 5 - ssh_FX_NO_CONNECTION = 6 - ssh_FX_CONNECTION_LOST = 7 - ssh_FX_OP_UNSUPPORTED = 8 - - // see draft-ietf-secsh-filexfer-13 - // https://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-9.1 - ssh_FX_INVALID_HANDLE = 9 - ssh_FX_NO_SUCH_PATH = 10 - ssh_FX_FILE_ALREADY_EXISTS = 11 - ssh_FX_WRITE_PROTECT = 12 - ssh_FX_NO_MEDIA = 13 - ssh_FX_NO_SPACE_ON_FILESYSTEM = 14 - ssh_FX_QUOTA_EXCEEDED = 15 - ssh_FX_UNKNOWN_PRINCIPAL = 16 - ssh_FX_LOCK_CONFLICT = 17 - ssh_FX_DIR_NOT_EMPTY = 18 - ssh_FX_NOT_A_DIRECTORY = 19 - ssh_FX_INVALID_FILENAME = 20 - ssh_FX_LINK_LOOP = 21 - ssh_FX_CANNOT_DELETE = 22 - ssh_FX_INVALID_PARAMETER = 23 - ssh_FX_FILE_IS_A_DIRECTORY = 24 - ssh_FX_BYTE_RANGE_LOCK_CONFLICT = 25 - ssh_FX_BYTE_RANGE_LOCK_REFUSED = 26 - ssh_FX_DELETE_PENDING = 27 - ssh_FX_FILE_CORRUPT = 28 - ssh_FX_OWNER_INVALID = 29 - ssh_FX_GROUP_INVALID = 30 - ssh_FX_NO_MATCHING_BYTE_RANGE_LOCK = 31 -) - -const ( - ssh_FXF_READ = 0x00000001 - ssh_FXF_WRITE = 0x00000002 - ssh_FXF_APPEND = 0x00000004 - ssh_FXF_CREAT = 0x00000008 - ssh_FXF_TRUNC = 0x00000010 - ssh_FXF_EXCL = 0x00000020 -) - -type fxp uint8 - -func (f fxp) String() string { - switch f { - case ssh_FXP_INIT: - return "SSH_FXP_INIT" - case ssh_FXP_VERSION: - return "SSH_FXP_VERSION" - case ssh_FXP_OPEN: - return "SSH_FXP_OPEN" - case ssh_FXP_CLOSE: - return "SSH_FXP_CLOSE" - case ssh_FXP_READ: - return "SSH_FXP_READ" - case ssh_FXP_WRITE: - return "SSH_FXP_WRITE" - case ssh_FXP_LSTAT: - return "SSH_FXP_LSTAT" - case ssh_FXP_FSTAT: - return "SSH_FXP_FSTAT" - case ssh_FXP_SETSTAT: - return "SSH_FXP_SETSTAT" - case ssh_FXP_FSETSTAT: - return "SSH_FXP_FSETSTAT" - case ssh_FXP_OPENDIR: - return "SSH_FXP_OPENDIR" - case ssh_FXP_READDIR: - return "SSH_FXP_READDIR" - case ssh_FXP_REMOVE: - return "SSH_FXP_REMOVE" - case ssh_FXP_MKDIR: - return "SSH_FXP_MKDIR" - case ssh_FXP_RMDIR: - return "SSH_FXP_RMDIR" - case ssh_FXP_REALPATH: - return "SSH_FXP_REALPATH" - case ssh_FXP_STAT: - return "SSH_FXP_STAT" - case ssh_FXP_RENAME: - return "SSH_FXP_RENAME" - case ssh_FXP_READLINK: - return "SSH_FXP_READLINK" - case ssh_FXP_SYMLINK: - return "SSH_FXP_SYMLINK" - case ssh_FXP_STATUS: - return "SSH_FXP_STATUS" - case ssh_FXP_HANDLE: - return "SSH_FXP_HANDLE" - case ssh_FXP_DATA: - return "SSH_FXP_DATA" - case ssh_FXP_NAME: - return "SSH_FXP_NAME" - case ssh_FXP_ATTRS: - return "SSH_FXP_ATTRS" - case ssh_FXP_EXTENDED: - return "SSH_FXP_EXTENDED" - case ssh_FXP_EXTENDED_REPLY: - return "SSH_FXP_EXTENDED_REPLY" - default: - return "unknown" - } -} - -type fx uint8 - -func (f fx) String() string { - switch f { - case ssh_FX_OK: - return "SSH_FX_OK" - case ssh_FX_EOF: - return "SSH_FX_EOF" - case ssh_FX_NO_SUCH_FILE: - return "SSH_FX_NO_SUCH_FILE" - case ssh_FX_PERMISSION_DENIED: - return "SSH_FX_PERMISSION_DENIED" - case ssh_FX_FAILURE: - return "SSH_FX_FAILURE" - case ssh_FX_BAD_MESSAGE: - return "SSH_FX_BAD_MESSAGE" - case ssh_FX_NO_CONNECTION: - return "SSH_FX_NO_CONNECTION" - case ssh_FX_CONNECTION_LOST: - return "SSH_FX_CONNECTION_LOST" - case ssh_FX_OP_UNSUPPORTED: - return "SSH_FX_OP_UNSUPPORTED" - default: - return "unknown" - } -} - -type unexpectedPacketErr struct { - want, got uint8 -} - -func (u *unexpectedPacketErr) Error() string { - return fmt.Sprintf("sftp: unexpected packet: want %v, got %v", fxp(u.want), fxp(u.got)) -} - -func unimplementedPacketErr(u uint8) error { - return errors.Errorf("sftp: unimplemented packet type: got %v", fxp(u)) -} - -type unexpectedIDErr struct{ want, got uint32 } - -func (u *unexpectedIDErr) Error() string { - return fmt.Sprintf("sftp: unexpected id: want %v, got %v", u.want, u.got) -} - -func unimplementedSeekWhence(whence int) error { - return errors.Errorf("sftp: unimplemented seek whence %v", whence) -} - -func unexpectedCount(want, got uint32) error { - return errors.Errorf("sftp: unexpected count: want %v, got %v", want, got) -} - -type unexpectedVersionErr struct{ want, got uint32 } - -func (u *unexpectedVersionErr) Error() string { - return fmt.Sprintf("sftp: unexpected server version: want %v, got %v", u.want, u.got) -} - -// A StatusError is returned when an SFTP operation fails, and provides -// additional information about the failure. -type StatusError struct { - Code uint32 - msg, lang string -} - -func (s *StatusError) Error() string { return fmt.Sprintf("sftp: %q (%v)", s.msg, fx(s.Code)) } diff --git a/vendor/github.com/kr/fs/LICENSE b/vendor/github.com/rogpeppe/go-internal/LICENSE similarity index 96% rename from vendor/github.com/kr/fs/LICENSE rename to vendor/github.com/rogpeppe/go-internal/LICENSE index 74487567632..49ea0f92882 100644 --- a/vendor/github.com/kr/fs/LICENSE +++ b/vendor/github.com/rogpeppe/go-internal/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2012 The Go Authors. All rights reserved. +Copyright (c) 2018 The Go Authors. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are diff --git a/vendor/github.com/rogpeppe/go-internal/modfile/BUILD.bazel b/vendor/github.com/rogpeppe/go-internal/modfile/BUILD.bazel new file mode 100644 index 00000000000..ff911dc6476 --- /dev/null +++ b/vendor/github.com/rogpeppe/go-internal/modfile/BUILD.bazel @@ -0,0 +1,18 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = [ + "gopkgin.go", + "print.go", + "read.go", + "rule.go", + ], + importmap = "sigs.k8s.io/cluster-api-provider-azure/vendor/github.com/rogpeppe/go-internal/modfile", + importpath = "github.com/rogpeppe/go-internal/modfile", + visibility = ["//visibility:public"], + deps = [ + "//vendor/github.com/rogpeppe/go-internal/module:go_default_library", + "//vendor/github.com/rogpeppe/go-internal/semver:go_default_library", + ], +) diff --git a/vendor/github.com/rogpeppe/go-internal/modfile/gopkgin.go b/vendor/github.com/rogpeppe/go-internal/modfile/gopkgin.go new file mode 100644 index 00000000000..c94b3848a0e --- /dev/null +++ b/vendor/github.com/rogpeppe/go-internal/modfile/gopkgin.go @@ -0,0 +1,47 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// TODO: Figure out what gopkg.in should do. + +package modfile + +import "strings" + +// ParseGopkgIn splits gopkg.in import paths into their constituent parts +func ParseGopkgIn(path string) (root, repo, major, subdir string, ok bool) { + if !strings.HasPrefix(path, "gopkg.in/") { + return + } + f := strings.Split(path, "/") + if len(f) >= 2 { + if elem, v, ok := dotV(f[1]); ok { + root = strings.Join(f[:2], "/") + repo = "github.com/go-" + elem + "/" + elem + major = v + subdir = strings.Join(f[2:], "/") + return root, repo, major, subdir, true + } + } + if len(f) >= 3 { + if elem, v, ok := dotV(f[2]); ok { + root = strings.Join(f[:3], "/") + repo = "github.com/" + f[1] + "/" + elem + major = v + subdir = strings.Join(f[3:], "/") + return root, repo, major, subdir, true + } + } + return +} + +func dotV(name string) (elem, v string, ok bool) { + i := len(name) - 1 + for i >= 0 && '0' <= name[i] && name[i] <= '9' { + i-- + } + if i <= 2 || i+1 >= len(name) || name[i-1] != '.' || name[i] != 'v' || name[i+1] == '0' && len(name) != i+2 { + return "", "", false + } + return name[:i-1], name[i:], true +} diff --git a/vendor/github.com/rogpeppe/go-internal/modfile/print.go b/vendor/github.com/rogpeppe/go-internal/modfile/print.go new file mode 100644 index 00000000000..7b1dd8f9533 --- /dev/null +++ b/vendor/github.com/rogpeppe/go-internal/modfile/print.go @@ -0,0 +1,164 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package modfile implements parsing and formatting for +// go.mod files. +package modfile + +import ( + "bytes" + "fmt" + "strings" +) + +func Format(f *FileSyntax) []byte { + pr := &printer{} + pr.file(f) + return pr.Bytes() +} + +// A printer collects the state during printing of a file or expression. +type printer struct { + bytes.Buffer // output buffer + comment []Comment // pending end-of-line comments + margin int // left margin (indent), a number of tabs +} + +// printf prints to the buffer. +func (p *printer) printf(format string, args ...interface{}) { + fmt.Fprintf(p, format, args...) +} + +// indent returns the position on the current line, in bytes, 0-indexed. +func (p *printer) indent() int { + b := p.Bytes() + n := 0 + for n < len(b) && b[len(b)-1-n] != '\n' { + n++ + } + return n +} + +// newline ends the current line, flushing end-of-line comments. +func (p *printer) newline() { + if len(p.comment) > 0 { + p.printf(" ") + for i, com := range p.comment { + if i > 0 { + p.trim() + p.printf("\n") + for i := 0; i < p.margin; i++ { + p.printf("\t") + } + } + p.printf("%s", strings.TrimSpace(com.Token)) + } + p.comment = p.comment[:0] + } + + p.trim() + p.printf("\n") + for i := 0; i < p.margin; i++ { + p.printf("\t") + } +} + +// trim removes trailing spaces and tabs from the current line. +func (p *printer) trim() { + // Remove trailing spaces and tabs from line we're about to end. + b := p.Bytes() + n := len(b) + for n > 0 && (b[n-1] == '\t' || b[n-1] == ' ') { + n-- + } + p.Truncate(n) +} + +// file formats the given file into the print buffer. +func (p *printer) file(f *FileSyntax) { + for _, com := range f.Before { + p.printf("%s", strings.TrimSpace(com.Token)) + p.newline() + } + + for i, stmt := range f.Stmt { + switch x := stmt.(type) { + case *CommentBlock: + // comments already handled + p.expr(x) + + default: + p.expr(x) + p.newline() + } + + for _, com := range stmt.Comment().After { + p.printf("%s", strings.TrimSpace(com.Token)) + p.newline() + } + + if i+1 < len(f.Stmt) { + p.newline() + } + } +} + +func (p *printer) expr(x Expr) { + // Emit line-comments preceding this expression. + if before := x.Comment().Before; len(before) > 0 { + // Want to print a line comment. + // Line comments must be at the current margin. + p.trim() + if p.indent() > 0 { + // There's other text on the line. Start a new line. + p.printf("\n") + } + // Re-indent to margin. + for i := 0; i < p.margin; i++ { + p.printf("\t") + } + for _, com := range before { + p.printf("%s", strings.TrimSpace(com.Token)) + p.newline() + } + } + + switch x := x.(type) { + default: + panic(fmt.Errorf("printer: unexpected type %T", x)) + + case *CommentBlock: + // done + + case *LParen: + p.printf("(") + case *RParen: + p.printf(")") + + case *Line: + sep := "" + for _, tok := range x.Token { + p.printf("%s%s", sep, tok) + sep = " " + } + + case *LineBlock: + for _, tok := range x.Token { + p.printf("%s ", tok) + } + p.expr(&x.LParen) + p.margin++ + for _, l := range x.Line { + p.newline() + p.expr(l) + } + p.margin-- + p.newline() + p.expr(&x.RParen) + } + + // Queue end-of-line comments for printing when we + // reach the end of the line. + p.comment = append(p.comment, x.Comment().Suffix...) +} diff --git a/vendor/github.com/rogpeppe/go-internal/modfile/read.go b/vendor/github.com/rogpeppe/go-internal/modfile/read.go new file mode 100644 index 00000000000..1d81ff1ab7a --- /dev/null +++ b/vendor/github.com/rogpeppe/go-internal/modfile/read.go @@ -0,0 +1,869 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Module file parser. +// This is a simplified copy of Google's buildifier parser. + +package modfile + +import ( + "bytes" + "fmt" + "os" + "strconv" + "strings" + "unicode" + "unicode/utf8" +) + +// A Position describes the position between two bytes of input. +type Position struct { + Line int // line in input (starting at 1) + LineRune int // rune in line (starting at 1) + Byte int // byte in input (starting at 0) +} + +// add returns the position at the end of s, assuming it starts at p. +func (p Position) add(s string) Position { + p.Byte += len(s) + if n := strings.Count(s, "\n"); n > 0 { + p.Line += n + s = s[strings.LastIndex(s, "\n")+1:] + p.LineRune = 1 + } + p.LineRune += utf8.RuneCountInString(s) + return p +} + +// An Expr represents an input element. +type Expr interface { + // Span returns the start and end position of the expression, + // excluding leading or trailing comments. + Span() (start, end Position) + + // Comment returns the comments attached to the expression. + // This method would normally be named 'Comments' but that + // would interfere with embedding a type of the same name. + Comment() *Comments +} + +// A Comment represents a single // comment. +type Comment struct { + Start Position + Token string // without trailing newline + Suffix bool // an end of line (not whole line) comment +} + +// Comments collects the comments associated with an expression. +type Comments struct { + Before []Comment // whole-line comments before this expression + Suffix []Comment // end-of-line comments after this expression + + // For top-level expressions only, After lists whole-line + // comments following the expression. + After []Comment +} + +// Comment returns the receiver. This isn't useful by itself, but +// a Comments struct is embedded into all the expression +// implementation types, and this gives each of those a Comment +// method to satisfy the Expr interface. +func (c *Comments) Comment() *Comments { + return c +} + +// A FileSyntax represents an entire go.mod file. +type FileSyntax struct { + Name string // file path + Comments + Stmt []Expr +} + +func (x *FileSyntax) Span() (start, end Position) { + if len(x.Stmt) == 0 { + return + } + start, _ = x.Stmt[0].Span() + _, end = x.Stmt[len(x.Stmt)-1].Span() + return start, end +} + +func (x *FileSyntax) addLine(hint Expr, tokens ...string) *Line { + if hint == nil { + // If no hint given, add to the last statement of the given type. + Loop: + for i := len(x.Stmt) - 1; i >= 0; i-- { + stmt := x.Stmt[i] + switch stmt := stmt.(type) { + case *Line: + if stmt.Token != nil && stmt.Token[0] == tokens[0] { + hint = stmt + break Loop + } + case *LineBlock: + if stmt.Token[0] == tokens[0] { + hint = stmt + break Loop + } + } + } + } + + if hint != nil { + for i, stmt := range x.Stmt { + switch stmt := stmt.(type) { + case *Line: + if stmt == hint { + // Convert line to line block. + stmt.InBlock = true + block := &LineBlock{Token: stmt.Token[:1], Line: []*Line{stmt}} + stmt.Token = stmt.Token[1:] + x.Stmt[i] = block + new := &Line{Token: tokens[1:], InBlock: true} + block.Line = append(block.Line, new) + return new + } + case *LineBlock: + if stmt == hint { + new := &Line{Token: tokens[1:], InBlock: true} + stmt.Line = append(stmt.Line, new) + return new + } + for j, line := range stmt.Line { + if line == hint { + // Add new line after hint. + stmt.Line = append(stmt.Line, nil) + copy(stmt.Line[j+2:], stmt.Line[j+1:]) + new := &Line{Token: tokens[1:], InBlock: true} + stmt.Line[j+1] = new + return new + } + } + } + } + } + + new := &Line{Token: tokens} + x.Stmt = append(x.Stmt, new) + return new +} + +func (x *FileSyntax) updateLine(line *Line, tokens ...string) { + if line.InBlock { + tokens = tokens[1:] + } + line.Token = tokens +} + +func (x *FileSyntax) removeLine(line *Line) { + line.Token = nil +} + +// Cleanup cleans up the file syntax x after any edit operations. +// To avoid quadratic behavior, removeLine marks the line as dead +// by setting line.Token = nil but does not remove it from the slice +// in which it appears. After edits have all been indicated, +// calling Cleanup cleans out the dead lines. +func (x *FileSyntax) Cleanup() { + w := 0 + for _, stmt := range x.Stmt { + switch stmt := stmt.(type) { + case *Line: + if stmt.Token == nil { + continue + } + case *LineBlock: + ww := 0 + for _, line := range stmt.Line { + if line.Token != nil { + stmt.Line[ww] = line + ww++ + } + } + if ww == 0 { + continue + } + if ww == 1 { + // Collapse block into single line. + line := &Line{ + Comments: Comments{ + Before: commentsAdd(stmt.Before, stmt.Line[0].Before), + Suffix: commentsAdd(stmt.Line[0].Suffix, stmt.Suffix), + After: commentsAdd(stmt.Line[0].After, stmt.After), + }, + Token: stringsAdd(stmt.Token, stmt.Line[0].Token), + } + x.Stmt[w] = line + w++ + continue + } + stmt.Line = stmt.Line[:ww] + } + x.Stmt[w] = stmt + w++ + } + x.Stmt = x.Stmt[:w] +} + +func commentsAdd(x, y []Comment) []Comment { + return append(x[:len(x):len(x)], y...) +} + +func stringsAdd(x, y []string) []string { + return append(x[:len(x):len(x)], y...) +} + +// A CommentBlock represents a top-level block of comments separate +// from any rule. +type CommentBlock struct { + Comments + Start Position +} + +func (x *CommentBlock) Span() (start, end Position) { + return x.Start, x.Start +} + +// A Line is a single line of tokens. +type Line struct { + Comments + Start Position + Token []string + InBlock bool + End Position +} + +func (x *Line) Span() (start, end Position) { + return x.Start, x.End +} + +// A LineBlock is a factored block of lines, like +// +// require ( +// "x" +// "y" +// ) +// +type LineBlock struct { + Comments + Start Position + LParen LParen + Token []string + Line []*Line + RParen RParen +} + +func (x *LineBlock) Span() (start, end Position) { + return x.Start, x.RParen.Pos.add(")") +} + +// An LParen represents the beginning of a parenthesized line block. +// It is a place to store suffix comments. +type LParen struct { + Comments + Pos Position +} + +func (x *LParen) Span() (start, end Position) { + return x.Pos, x.Pos.add(")") +} + +// An RParen represents the end of a parenthesized line block. +// It is a place to store whole-line (before) comments. +type RParen struct { + Comments + Pos Position +} + +func (x *RParen) Span() (start, end Position) { + return x.Pos, x.Pos.add(")") +} + +// An input represents a single input file being parsed. +type input struct { + // Lexing state. + filename string // name of input file, for errors + complete []byte // entire input + remaining []byte // remaining input + token []byte // token being scanned + lastToken string // most recently returned token, for error messages + pos Position // current input position + comments []Comment // accumulated comments + endRule int // position of end of current rule + + // Parser state. + file *FileSyntax // returned top-level syntax tree + parseError error // error encountered during parsing + + // Comment assignment state. + pre []Expr // all expressions, in preorder traversal + post []Expr // all expressions, in postorder traversal +} + +func newInput(filename string, data []byte) *input { + return &input{ + filename: filename, + complete: data, + remaining: data, + pos: Position{Line: 1, LineRune: 1, Byte: 0}, + } +} + +// parse parses the input file. +func parse(file string, data []byte) (f *FileSyntax, err error) { + in := newInput(file, data) + // The parser panics for both routine errors like syntax errors + // and for programmer bugs like array index errors. + // Turn both into error returns. Catching bug panics is + // especially important when processing many files. + defer func() { + if e := recover(); e != nil { + if e == in.parseError { + err = in.parseError + } else { + err = fmt.Errorf("%s:%d:%d: internal error: %v", in.filename, in.pos.Line, in.pos.LineRune, e) + } + } + }() + + // Invoke the parser. + in.parseFile() + if in.parseError != nil { + return nil, in.parseError + } + in.file.Name = in.filename + + // Assign comments to nearby syntax. + in.assignComments() + + return in.file, nil +} + +// Error is called to report an error. +// The reason s is often "syntax error". +// Error does not return: it panics. +func (in *input) Error(s string) { + if s == "syntax error" && in.lastToken != "" { + s += " near " + in.lastToken + } + in.parseError = fmt.Errorf("%s:%d:%d: %v", in.filename, in.pos.Line, in.pos.LineRune, s) + panic(in.parseError) +} + +// eof reports whether the input has reached end of file. +func (in *input) eof() bool { + return len(in.remaining) == 0 +} + +// peekRune returns the next rune in the input without consuming it. +func (in *input) peekRune() int { + if len(in.remaining) == 0 { + return 0 + } + r, _ := utf8.DecodeRune(in.remaining) + return int(r) +} + +// peekPrefix reports whether the remaining input begins with the given prefix. +func (in *input) peekPrefix(prefix string) bool { + // This is like bytes.HasPrefix(in.remaining, []byte(prefix)) + // but without the allocation of the []byte copy of prefix. + for i := 0; i < len(prefix); i++ { + if i >= len(in.remaining) || in.remaining[i] != prefix[i] { + return false + } + } + return true +} + +// readRune consumes and returns the next rune in the input. +func (in *input) readRune() int { + if len(in.remaining) == 0 { + in.Error("internal lexer error: readRune at EOF") + } + r, size := utf8.DecodeRune(in.remaining) + in.remaining = in.remaining[size:] + if r == '\n' { + in.pos.Line++ + in.pos.LineRune = 1 + } else { + in.pos.LineRune++ + } + in.pos.Byte += size + return int(r) +} + +type symType struct { + pos Position + endPos Position + text string +} + +// startToken marks the beginning of the next input token. +// It must be followed by a call to endToken, once the token has +// been consumed using readRune. +func (in *input) startToken(sym *symType) { + in.token = in.remaining + sym.text = "" + sym.pos = in.pos +} + +// endToken marks the end of an input token. +// It records the actual token string in sym.text if the caller +// has not done that already. +func (in *input) endToken(sym *symType) { + if sym.text == "" { + tok := string(in.token[:len(in.token)-len(in.remaining)]) + sym.text = tok + in.lastToken = sym.text + } + sym.endPos = in.pos +} + +// lex is called from the parser to obtain the next input token. +// It returns the token value (either a rune like '+' or a symbolic token _FOR) +// and sets val to the data associated with the token. +// For all our input tokens, the associated data is +// val.Pos (the position where the token begins) +// and val.Token (the input string corresponding to the token). +func (in *input) lex(sym *symType) int { + // Skip past spaces, stopping at non-space or EOF. + countNL := 0 // number of newlines we've skipped past + for !in.eof() { + // Skip over spaces. Count newlines so we can give the parser + // information about where top-level blank lines are, + // for top-level comment assignment. + c := in.peekRune() + if c == ' ' || c == '\t' || c == '\r' { + in.readRune() + continue + } + + // Comment runs to end of line. + if in.peekPrefix("//") { + in.startToken(sym) + + // Is this comment the only thing on its line? + // Find the last \n before this // and see if it's all + // spaces from there to here. + i := bytes.LastIndex(in.complete[:in.pos.Byte], []byte("\n")) + suffix := len(bytes.TrimSpace(in.complete[i+1:in.pos.Byte])) > 0 + in.readRune() + in.readRune() + + // Consume comment. + for len(in.remaining) > 0 && in.readRune() != '\n' { + } + in.endToken(sym) + + sym.text = strings.TrimRight(sym.text, "\n") + in.lastToken = "comment" + + // If we are at top level (not in a statement), hand the comment to + // the parser as a _COMMENT token. The grammar is written + // to handle top-level comments itself. + if !suffix { + // Not in a statement. Tell parser about top-level comment. + return _COMMENT + } + + // Otherwise, save comment for later attachment to syntax tree. + if countNL > 1 { + in.comments = append(in.comments, Comment{sym.pos, "", false}) + } + in.comments = append(in.comments, Comment{sym.pos, sym.text, suffix}) + countNL = 1 + return _EOL + } + + if in.peekPrefix("/*") { + in.Error(fmt.Sprintf("mod files must use // comments (not /* */ comments)")) + } + + // Found non-space non-comment. + break + } + + // Found the beginning of the next token. + in.startToken(sym) + defer in.endToken(sym) + + // End of file. + if in.eof() { + in.lastToken = "EOF" + return _EOF + } + + // Punctuation tokens. + switch c := in.peekRune(); c { + case '\n': + in.readRune() + return c + + case '(': + in.readRune() + return c + + case ')': + in.readRune() + return c + + case '"', '`': // quoted string + quote := c + in.readRune() + for { + if in.eof() { + in.pos = sym.pos + in.Error("unexpected EOF in string") + } + if in.peekRune() == '\n' { + in.Error("unexpected newline in string") + } + c := in.readRune() + if c == quote { + break + } + if c == '\\' && quote != '`' { + if in.eof() { + in.pos = sym.pos + in.Error("unexpected EOF in string") + } + in.readRune() + } + } + in.endToken(sym) + return _STRING + } + + // Checked all punctuation. Must be identifier token. + if c := in.peekRune(); !isIdent(c) { + in.Error(fmt.Sprintf("unexpected input character %#q", c)) + } + + // Scan over identifier. + for isIdent(in.peekRune()) { + if in.peekPrefix("//") { + break + } + if in.peekPrefix("/*") { + in.Error(fmt.Sprintf("mod files must use // comments (not /* */ comments)")) + } + in.readRune() + } + return _IDENT +} + +// isIdent reports whether c is an identifier rune. +// We treat nearly all runes as identifier runes. +func isIdent(c int) bool { + return c != 0 && !unicode.IsSpace(rune(c)) +} + +// Comment assignment. +// We build two lists of all subexpressions, preorder and postorder. +// The preorder list is ordered by start location, with outer expressions first. +// The postorder list is ordered by end location, with outer expressions last. +// We use the preorder list to assign each whole-line comment to the syntax +// immediately following it, and we use the postorder list to assign each +// end-of-line comment to the syntax immediately preceding it. + +// order walks the expression adding it and its subexpressions to the +// preorder and postorder lists. +func (in *input) order(x Expr) { + if x != nil { + in.pre = append(in.pre, x) + } + switch x := x.(type) { + default: + panic(fmt.Errorf("order: unexpected type %T", x)) + case nil: + // nothing + case *LParen, *RParen: + // nothing + case *CommentBlock: + // nothing + case *Line: + // nothing + case *FileSyntax: + for _, stmt := range x.Stmt { + in.order(stmt) + } + case *LineBlock: + in.order(&x.LParen) + for _, l := range x.Line { + in.order(l) + } + in.order(&x.RParen) + } + if x != nil { + in.post = append(in.post, x) + } +} + +// assignComments attaches comments to nearby syntax. +func (in *input) assignComments() { + const debug = false + + // Generate preorder and postorder lists. + in.order(in.file) + + // Split into whole-line comments and suffix comments. + var line, suffix []Comment + for _, com := range in.comments { + if com.Suffix { + suffix = append(suffix, com) + } else { + line = append(line, com) + } + } + + if debug { + for _, c := range line { + fmt.Fprintf(os.Stderr, "LINE %q :%d:%d #%d\n", c.Token, c.Start.Line, c.Start.LineRune, c.Start.Byte) + } + } + + // Assign line comments to syntax immediately following. + for _, x := range in.pre { + start, _ := x.Span() + if debug { + fmt.Printf("pre %T :%d:%d #%d\n", x, start.Line, start.LineRune, start.Byte) + } + xcom := x.Comment() + for len(line) > 0 && start.Byte >= line[0].Start.Byte { + if debug { + fmt.Fprintf(os.Stderr, "ASSIGN LINE %q #%d\n", line[0].Token, line[0].Start.Byte) + } + xcom.Before = append(xcom.Before, line[0]) + line = line[1:] + } + } + + // Remaining line comments go at end of file. + in.file.After = append(in.file.After, line...) + + if debug { + for _, c := range suffix { + fmt.Fprintf(os.Stderr, "SUFFIX %q :%d:%d #%d\n", c.Token, c.Start.Line, c.Start.LineRune, c.Start.Byte) + } + } + + // Assign suffix comments to syntax immediately before. + for i := len(in.post) - 1; i >= 0; i-- { + x := in.post[i] + + start, end := x.Span() + if debug { + fmt.Printf("post %T :%d:%d #%d :%d:%d #%d\n", x, start.Line, start.LineRune, start.Byte, end.Line, end.LineRune, end.Byte) + } + + // Do not assign suffix comments to end of line block or whole file. + // Instead assign them to the last element inside. + switch x.(type) { + case *FileSyntax: + continue + } + + // Do not assign suffix comments to something that starts + // on an earlier line, so that in + // + // x ( y + // z ) // comment + // + // we assign the comment to z and not to x ( ... ). + if start.Line != end.Line { + continue + } + xcom := x.Comment() + for len(suffix) > 0 && end.Byte <= suffix[len(suffix)-1].Start.Byte { + if debug { + fmt.Fprintf(os.Stderr, "ASSIGN SUFFIX %q #%d\n", suffix[len(suffix)-1].Token, suffix[len(suffix)-1].Start.Byte) + } + xcom.Suffix = append(xcom.Suffix, suffix[len(suffix)-1]) + suffix = suffix[:len(suffix)-1] + } + } + + // We assigned suffix comments in reverse. + // If multiple suffix comments were appended to the same + // expression node, they are now in reverse. Fix that. + for _, x := range in.post { + reverseComments(x.Comment().Suffix) + } + + // Remaining suffix comments go at beginning of file. + in.file.Before = append(in.file.Before, suffix...) +} + +// reverseComments reverses the []Comment list. +func reverseComments(list []Comment) { + for i, j := 0, len(list)-1; i < j; i, j = i+1, j-1 { + list[i], list[j] = list[j], list[i] + } +} + +func (in *input) parseFile() { + in.file = new(FileSyntax) + var sym symType + var cb *CommentBlock + for { + tok := in.lex(&sym) + switch tok { + case '\n': + if cb != nil { + in.file.Stmt = append(in.file.Stmt, cb) + cb = nil + } + case _COMMENT: + if cb == nil { + cb = &CommentBlock{Start: sym.pos} + } + com := cb.Comment() + com.Before = append(com.Before, Comment{Start: sym.pos, Token: sym.text}) + case _EOF: + if cb != nil { + in.file.Stmt = append(in.file.Stmt, cb) + } + return + default: + in.parseStmt(&sym) + if cb != nil { + in.file.Stmt[len(in.file.Stmt)-1].Comment().Before = cb.Before + cb = nil + } + } + } +} + +func (in *input) parseStmt(sym *symType) { + start := sym.pos + end := sym.endPos + token := []string{sym.text} + for { + tok := in.lex(sym) + switch tok { + case '\n', _EOF, _EOL: + in.file.Stmt = append(in.file.Stmt, &Line{ + Start: start, + Token: token, + End: end, + }) + return + case '(': + in.file.Stmt = append(in.file.Stmt, in.parseLineBlock(start, token, sym)) + return + default: + token = append(token, sym.text) + end = sym.endPos + } + } +} + +func (in *input) parseLineBlock(start Position, token []string, sym *symType) *LineBlock { + x := &LineBlock{ + Start: start, + Token: token, + LParen: LParen{Pos: sym.pos}, + } + var comments []Comment + for { + tok := in.lex(sym) + switch tok { + case _EOL: + // ignore + case '\n': + if len(comments) == 0 && len(x.Line) > 0 || len(comments) > 0 && comments[len(comments)-1].Token != "" { + comments = append(comments, Comment{}) + } + case _COMMENT: + comments = append(comments, Comment{Start: sym.pos, Token: sym.text}) + case _EOF: + in.Error(fmt.Sprintf("syntax error (unterminated block started at %s:%d:%d)", in.filename, x.Start.Line, x.Start.LineRune)) + case ')': + x.RParen.Before = comments + x.RParen.Pos = sym.pos + tok = in.lex(sym) + if tok != '\n' && tok != _EOF && tok != _EOL { + in.Error("syntax error (expected newline after closing paren)") + } + return x + default: + l := in.parseLine(sym) + x.Line = append(x.Line, l) + l.Comment().Before = comments + comments = nil + } + } +} + +func (in *input) parseLine(sym *symType) *Line { + start := sym.pos + end := sym.endPos + token := []string{sym.text} + for { + tok := in.lex(sym) + switch tok { + case '\n', _EOF, _EOL: + return &Line{ + Start: start, + Token: token, + End: end, + InBlock: true, + } + default: + token = append(token, sym.text) + end = sym.endPos + } + } +} + +const ( + _EOF = -(1 + iota) + _EOL + _IDENT + _STRING + _COMMENT +) + +var ( + slashSlash = []byte("//") + moduleStr = []byte("module") +) + +// ModulePath returns the module path from the gomod file text. +// If it cannot find a module path, it returns an empty string. +// It is tolerant of unrelated problems in the go.mod file. +func ModulePath(mod []byte) string { + for len(mod) > 0 { + line := mod + mod = nil + if i := bytes.IndexByte(line, '\n'); i >= 0 { + line, mod = line[:i], line[i+1:] + } + if i := bytes.Index(line, slashSlash); i >= 0 { + line = line[:i] + } + line = bytes.TrimSpace(line) + if !bytes.HasPrefix(line, moduleStr) { + continue + } + line = line[len(moduleStr):] + n := len(line) + line = bytes.TrimSpace(line) + if len(line) == n || len(line) == 0 { + continue + } + + if line[0] == '"' || line[0] == '`' { + p, err := strconv.Unquote(string(line)) + if err != nil { + return "" // malformed quoted string or multiline module path + } + return p + } + + return string(line) + } + return "" // missing module path +} diff --git a/vendor/github.com/rogpeppe/go-internal/modfile/rule.go b/vendor/github.com/rogpeppe/go-internal/modfile/rule.go new file mode 100644 index 00000000000..24d275f12f2 --- /dev/null +++ b/vendor/github.com/rogpeppe/go-internal/modfile/rule.go @@ -0,0 +1,724 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package modfile + +import ( + "bytes" + "errors" + "fmt" + "path/filepath" + "regexp" + "sort" + "strconv" + "strings" + "unicode" + + "github.com/rogpeppe/go-internal/module" + "github.com/rogpeppe/go-internal/semver" +) + +// A File is the parsed, interpreted form of a go.mod file. +type File struct { + Module *Module + Go *Go + Require []*Require + Exclude []*Exclude + Replace []*Replace + + Syntax *FileSyntax +} + +// A Module is the module statement. +type Module struct { + Mod module.Version + Syntax *Line +} + +// A Go is the go statement. +type Go struct { + Version string // "1.23" + Syntax *Line +} + +// A Require is a single require statement. +type Require struct { + Mod module.Version + Indirect bool // has "// indirect" comment + Syntax *Line +} + +// An Exclude is a single exclude statement. +type Exclude struct { + Mod module.Version + Syntax *Line +} + +// A Replace is a single replace statement. +type Replace struct { + Old module.Version + New module.Version + Syntax *Line +} + +func (f *File) AddModuleStmt(path string) error { + if f.Syntax == nil { + f.Syntax = new(FileSyntax) + } + if f.Module == nil { + f.Module = &Module{ + Mod: module.Version{Path: path}, + Syntax: f.Syntax.addLine(nil, "module", AutoQuote(path)), + } + } else { + f.Module.Mod.Path = path + f.Syntax.updateLine(f.Module.Syntax, "module", AutoQuote(path)) + } + return nil +} + +func (f *File) AddComment(text string) { + if f.Syntax == nil { + f.Syntax = new(FileSyntax) + } + f.Syntax.Stmt = append(f.Syntax.Stmt, &CommentBlock{ + Comments: Comments{ + Before: []Comment{ + { + Token: text, + }, + }, + }, + }) +} + +type VersionFixer func(path, version string) (string, error) + +// Parse parses the data, reported in errors as being from file, +// into a File struct. It applies fix, if non-nil, to canonicalize all module versions found. +func Parse(file string, data []byte, fix VersionFixer) (*File, error) { + return parseToFile(file, data, fix, true) +} + +// ParseLax is like Parse but ignores unknown statements. +// It is used when parsing go.mod files other than the main module, +// under the theory that most statement types we add in the future will +// only apply in the main module, like exclude and replace, +// and so we get better gradual deployments if old go commands +// simply ignore those statements when found in go.mod files +// in dependencies. +func ParseLax(file string, data []byte, fix VersionFixer) (*File, error) { + return parseToFile(file, data, fix, false) +} + +func parseToFile(file string, data []byte, fix VersionFixer, strict bool) (*File, error) { + fs, err := parse(file, data) + if err != nil { + return nil, err + } + f := &File{ + Syntax: fs, + } + + var errs bytes.Buffer + for _, x := range fs.Stmt { + switch x := x.(type) { + case *Line: + f.add(&errs, x, x.Token[0], x.Token[1:], fix, strict) + + case *LineBlock: + if len(x.Token) > 1 { + if strict { + fmt.Fprintf(&errs, "%s:%d: unknown block type: %s\n", file, x.Start.Line, strings.Join(x.Token, " ")) + } + continue + } + switch x.Token[0] { + default: + if strict { + fmt.Fprintf(&errs, "%s:%d: unknown block type: %s\n", file, x.Start.Line, strings.Join(x.Token, " ")) + } + continue + case "module", "require", "exclude", "replace": + for _, l := range x.Line { + f.add(&errs, l, x.Token[0], l.Token, fix, strict) + } + } + } + } + + if errs.Len() > 0 { + return nil, errors.New(strings.TrimRight(errs.String(), "\n")) + } + return f, nil +} + +var goVersionRE = regexp.MustCompile(`([1-9][0-9]*)\.(0|[1-9][0-9]*)`) + +func (f *File) add(errs *bytes.Buffer, line *Line, verb string, args []string, fix VersionFixer, strict bool) { + // If strict is false, this module is a dependency. + // We ignore all unknown directives as well as main-module-only + // directives like replace and exclude. It will work better for + // forward compatibility if we can depend on modules that have unknown + // statements (presumed relevant only when acting as the main module) + // and simply ignore those statements. + if !strict { + switch verb { + case "module", "require", "go": + // want these even for dependency go.mods + default: + return + } + } + + switch verb { + default: + fmt.Fprintf(errs, "%s:%d: unknown directive: %s\n", f.Syntax.Name, line.Start.Line, verb) + + case "go": + if f.Go != nil { + fmt.Fprintf(errs, "%s:%d: repeated go statement\n", f.Syntax.Name, line.Start.Line) + return + } + if len(args) != 1 || !goVersionRE.MatchString(args[0]) { + fmt.Fprintf(errs, "%s:%d: usage: go 1.23\n", f.Syntax.Name, line.Start.Line) + return + } + f.Go = &Go{Syntax: line} + f.Go.Version = args[0] + case "module": + if f.Module != nil { + fmt.Fprintf(errs, "%s:%d: repeated module statement\n", f.Syntax.Name, line.Start.Line) + return + } + f.Module = &Module{Syntax: line} + if len(args) != 1 { + + fmt.Fprintf(errs, "%s:%d: usage: module module/path [version]\n", f.Syntax.Name, line.Start.Line) + return + } + s, err := parseString(&args[0]) + if err != nil { + fmt.Fprintf(errs, "%s:%d: invalid quoted string: %v\n", f.Syntax.Name, line.Start.Line, err) + return + } + f.Module.Mod = module.Version{Path: s} + case "require", "exclude": + if len(args) != 2 { + fmt.Fprintf(errs, "%s:%d: usage: %s module/path v1.2.3\n", f.Syntax.Name, line.Start.Line, verb) + return + } + s, err := parseString(&args[0]) + if err != nil { + fmt.Fprintf(errs, "%s:%d: invalid quoted string: %v\n", f.Syntax.Name, line.Start.Line, err) + return + } + old := args[1] + v, err := parseVersion(s, &args[1], fix) + if err != nil { + fmt.Fprintf(errs, "%s:%d: invalid module version %q: %v\n", f.Syntax.Name, line.Start.Line, old, err) + return + } + pathMajor, err := modulePathMajor(s) + if err != nil { + fmt.Fprintf(errs, "%s:%d: %v\n", f.Syntax.Name, line.Start.Line, err) + return + } + if !module.MatchPathMajor(v, pathMajor) { + if pathMajor == "" { + pathMajor = "v0 or v1" + } + fmt.Fprintf(errs, "%s:%d: invalid module: %s should be %s, not %s (%s)\n", f.Syntax.Name, line.Start.Line, s, pathMajor, semver.Major(v), v) + return + } + if verb == "require" { + f.Require = append(f.Require, &Require{ + Mod: module.Version{Path: s, Version: v}, + Syntax: line, + Indirect: isIndirect(line), + }) + } else { + f.Exclude = append(f.Exclude, &Exclude{ + Mod: module.Version{Path: s, Version: v}, + Syntax: line, + }) + } + case "replace": + arrow := 2 + if len(args) >= 2 && args[1] == "=>" { + arrow = 1 + } + if len(args) < arrow+2 || len(args) > arrow+3 || args[arrow] != "=>" { + fmt.Fprintf(errs, "%s:%d: usage: %s module/path [v1.2.3] => other/module v1.4\n\t or %s module/path [v1.2.3] => ../local/directory\n", f.Syntax.Name, line.Start.Line, verb, verb) + return + } + s, err := parseString(&args[0]) + if err != nil { + fmt.Fprintf(errs, "%s:%d: invalid quoted string: %v\n", f.Syntax.Name, line.Start.Line, err) + return + } + pathMajor, err := modulePathMajor(s) + if err != nil { + fmt.Fprintf(errs, "%s:%d: %v\n", f.Syntax.Name, line.Start.Line, err) + return + } + var v string + if arrow == 2 { + old := args[1] + v, err = parseVersion(s, &args[1], fix) + if err != nil { + fmt.Fprintf(errs, "%s:%d: invalid module version %v: %v\n", f.Syntax.Name, line.Start.Line, old, err) + return + } + if !module.MatchPathMajor(v, pathMajor) { + if pathMajor == "" { + pathMajor = "v0 or v1" + } + fmt.Fprintf(errs, "%s:%d: invalid module: %s should be %s, not %s (%s)\n", f.Syntax.Name, line.Start.Line, s, pathMajor, semver.Major(v), v) + return + } + } + ns, err := parseString(&args[arrow+1]) + if err != nil { + fmt.Fprintf(errs, "%s:%d: invalid quoted string: %v\n", f.Syntax.Name, line.Start.Line, err) + return + } + nv := "" + if len(args) == arrow+2 { + if !IsDirectoryPath(ns) { + fmt.Fprintf(errs, "%s:%d: replacement module without version must be directory path (rooted or starting with ./ or ../)\n", f.Syntax.Name, line.Start.Line) + return + } + if filepath.Separator == '/' && strings.Contains(ns, `\`) { + fmt.Fprintf(errs, "%s:%d: replacement directory appears to be Windows path (on a non-windows system)\n", f.Syntax.Name, line.Start.Line) + return + } + } + if len(args) == arrow+3 { + old := args[arrow+1] + nv, err = parseVersion(ns, &args[arrow+2], fix) + if err != nil { + fmt.Fprintf(errs, "%s:%d: invalid module version %v: %v\n", f.Syntax.Name, line.Start.Line, old, err) + return + } + if IsDirectoryPath(ns) { + fmt.Fprintf(errs, "%s:%d: replacement module directory path %q cannot have version\n", f.Syntax.Name, line.Start.Line, ns) + return + } + } + f.Replace = append(f.Replace, &Replace{ + Old: module.Version{Path: s, Version: v}, + New: module.Version{Path: ns, Version: nv}, + Syntax: line, + }) + } +} + +// isIndirect reports whether line has a "// indirect" comment, +// meaning it is in go.mod only for its effect on indirect dependencies, +// so that it can be dropped entirely once the effective version of the +// indirect dependency reaches the given minimum version. +func isIndirect(line *Line) bool { + if len(line.Suffix) == 0 { + return false + } + f := strings.Fields(line.Suffix[0].Token) + return (len(f) == 2 && f[1] == "indirect" || len(f) > 2 && f[1] == "indirect;") && f[0] == "//" +} + +// setIndirect sets line to have (or not have) a "// indirect" comment. +func setIndirect(line *Line, indirect bool) { + if isIndirect(line) == indirect { + return + } + if indirect { + // Adding comment. + if len(line.Suffix) == 0 { + // New comment. + line.Suffix = []Comment{{Token: "// indirect", Suffix: true}} + return + } + // Insert at beginning of existing comment. + com := &line.Suffix[0] + space := " " + if len(com.Token) > 2 && com.Token[2] == ' ' || com.Token[2] == '\t' { + space = "" + } + com.Token = "// indirect;" + space + com.Token[2:] + return + } + + // Removing comment. + f := strings.Fields(line.Suffix[0].Token) + if len(f) == 2 { + // Remove whole comment. + line.Suffix = nil + return + } + + // Remove comment prefix. + com := &line.Suffix[0] + i := strings.Index(com.Token, "indirect;") + com.Token = "//" + com.Token[i+len("indirect;"):] +} + +// IsDirectoryPath reports whether the given path should be interpreted +// as a directory path. Just like on the go command line, relative paths +// and rooted paths are directory paths; the rest are module paths. +func IsDirectoryPath(ns string) bool { + // Because go.mod files can move from one system to another, + // we check all known path syntaxes, both Unix and Windows. + return strings.HasPrefix(ns, "./") || strings.HasPrefix(ns, "../") || strings.HasPrefix(ns, "/") || + strings.HasPrefix(ns, `.\`) || strings.HasPrefix(ns, `..\`) || strings.HasPrefix(ns, `\`) || + len(ns) >= 2 && ('A' <= ns[0] && ns[0] <= 'Z' || 'a' <= ns[0] && ns[0] <= 'z') && ns[1] == ':' +} + +// MustQuote reports whether s must be quoted in order to appear as +// a single token in a go.mod line. +func MustQuote(s string) bool { + for _, r := range s { + if !unicode.IsPrint(r) || r == ' ' || r == '"' || r == '\'' || r == '`' { + return true + } + } + return s == "" || strings.Contains(s, "//") || strings.Contains(s, "/*") +} + +// AutoQuote returns s or, if quoting is required for s to appear in a go.mod, +// the quotation of s. +func AutoQuote(s string) string { + if MustQuote(s) { + return strconv.Quote(s) + } + return s +} + +func parseString(s *string) (string, error) { + t := *s + if strings.HasPrefix(t, `"`) { + var err error + if t, err = strconv.Unquote(t); err != nil { + return "", err + } + } else if strings.ContainsAny(t, "\"'`") { + // Other quotes are reserved both for possible future expansion + // and to avoid confusion. For example if someone types 'x' + // we want that to be a syntax error and not a literal x in literal quotation marks. + return "", fmt.Errorf("unquoted string cannot contain quote") + } + *s = AutoQuote(t) + return t, nil +} + +func parseVersion(path string, s *string, fix VersionFixer) (string, error) { + t, err := parseString(s) + if err != nil { + return "", err + } + if fix != nil { + var err error + t, err = fix(path, t) + if err != nil { + return "", err + } + } + if v := module.CanonicalVersion(t); v != "" { + *s = v + return *s, nil + } + return "", fmt.Errorf("version must be of the form v1.2.3") +} + +func modulePathMajor(path string) (string, error) { + _, major, ok := module.SplitPathVersion(path) + if !ok { + return "", fmt.Errorf("invalid module path") + } + return major, nil +} + +func (f *File) Format() ([]byte, error) { + return Format(f.Syntax), nil +} + +// Cleanup cleans up the file f after any edit operations. +// To avoid quadratic behavior, modifications like DropRequire +// clear the entry but do not remove it from the slice. +// Cleanup cleans out all the cleared entries. +func (f *File) Cleanup() { + w := 0 + for _, r := range f.Require { + if r.Mod.Path != "" { + f.Require[w] = r + w++ + } + } + f.Require = f.Require[:w] + + w = 0 + for _, x := range f.Exclude { + if x.Mod.Path != "" { + f.Exclude[w] = x + w++ + } + } + f.Exclude = f.Exclude[:w] + + w = 0 + for _, r := range f.Replace { + if r.Old.Path != "" { + f.Replace[w] = r + w++ + } + } + f.Replace = f.Replace[:w] + + f.Syntax.Cleanup() +} + +func (f *File) AddRequire(path, vers string) error { + need := true + for _, r := range f.Require { + if r.Mod.Path == path { + if need { + r.Mod.Version = vers + f.Syntax.updateLine(r.Syntax, "require", AutoQuote(path), vers) + need = false + } else { + f.Syntax.removeLine(r.Syntax) + *r = Require{} + } + } + } + + if need { + f.AddNewRequire(path, vers, false) + } + return nil +} + +func (f *File) AddNewRequire(path, vers string, indirect bool) { + line := f.Syntax.addLine(nil, "require", AutoQuote(path), vers) + setIndirect(line, indirect) + f.Require = append(f.Require, &Require{module.Version{Path: path, Version: vers}, indirect, line}) +} + +func (f *File) SetRequire(req []*Require) { + need := make(map[string]string) + indirect := make(map[string]bool) + for _, r := range req { + need[r.Mod.Path] = r.Mod.Version + indirect[r.Mod.Path] = r.Indirect + } + + for _, r := range f.Require { + if v, ok := need[r.Mod.Path]; ok { + r.Mod.Version = v + r.Indirect = indirect[r.Mod.Path] + } + } + + var newStmts []Expr + for _, stmt := range f.Syntax.Stmt { + switch stmt := stmt.(type) { + case *LineBlock: + if len(stmt.Token) > 0 && stmt.Token[0] == "require" { + var newLines []*Line + for _, line := range stmt.Line { + if p, err := parseString(&line.Token[0]); err == nil && need[p] != "" { + line.Token[1] = need[p] + delete(need, p) + setIndirect(line, indirect[p]) + newLines = append(newLines, line) + } + } + if len(newLines) == 0 { + continue // drop stmt + } + stmt.Line = newLines + } + + case *Line: + if len(stmt.Token) > 0 && stmt.Token[0] == "require" { + if p, err := parseString(&stmt.Token[1]); err == nil && need[p] != "" { + stmt.Token[2] = need[p] + delete(need, p) + setIndirect(stmt, indirect[p]) + } else { + continue // drop stmt + } + } + } + newStmts = append(newStmts, stmt) + } + f.Syntax.Stmt = newStmts + + for path, vers := range need { + f.AddNewRequire(path, vers, indirect[path]) + } + f.SortBlocks() +} + +func (f *File) DropRequire(path string) error { + for _, r := range f.Require { + if r.Mod.Path == path { + f.Syntax.removeLine(r.Syntax) + *r = Require{} + } + } + return nil +} + +func (f *File) AddExclude(path, vers string) error { + var hint *Line + for _, x := range f.Exclude { + if x.Mod.Path == path && x.Mod.Version == vers { + return nil + } + if x.Mod.Path == path { + hint = x.Syntax + } + } + + f.Exclude = append(f.Exclude, &Exclude{Mod: module.Version{Path: path, Version: vers}, Syntax: f.Syntax.addLine(hint, "exclude", AutoQuote(path), vers)}) + return nil +} + +func (f *File) DropExclude(path, vers string) error { + for _, x := range f.Exclude { + if x.Mod.Path == path && x.Mod.Version == vers { + f.Syntax.removeLine(x.Syntax) + *x = Exclude{} + } + } + return nil +} + +func (f *File) AddReplace(oldPath, oldVers, newPath, newVers string) error { + need := true + old := module.Version{Path: oldPath, Version: oldVers} + new := module.Version{Path: newPath, Version: newVers} + tokens := []string{"replace", AutoQuote(oldPath)} + if oldVers != "" { + tokens = append(tokens, oldVers) + } + tokens = append(tokens, "=>", AutoQuote(newPath)) + if newVers != "" { + tokens = append(tokens, newVers) + } + + var hint *Line + for _, r := range f.Replace { + if r.Old.Path == oldPath && (oldVers == "" || r.Old.Version == oldVers) { + if need { + // Found replacement for old; update to use new. + r.New = new + f.Syntax.updateLine(r.Syntax, tokens...) + need = false + continue + } + // Already added; delete other replacements for same. + f.Syntax.removeLine(r.Syntax) + *r = Replace{} + } + if r.Old.Path == oldPath { + hint = r.Syntax + } + } + if need { + f.Replace = append(f.Replace, &Replace{Old: old, New: new, Syntax: f.Syntax.addLine(hint, tokens...)}) + } + return nil +} + +func (f *File) DropReplace(oldPath, oldVers string) error { + for _, r := range f.Replace { + if r.Old.Path == oldPath && r.Old.Version == oldVers { + f.Syntax.removeLine(r.Syntax) + *r = Replace{} + } + } + return nil +} + +func (f *File) SortBlocks() { + f.removeDups() // otherwise sorting is unsafe + + for _, stmt := range f.Syntax.Stmt { + block, ok := stmt.(*LineBlock) + if !ok { + continue + } + sort.Slice(block.Line, func(i, j int) bool { + li := block.Line[i] + lj := block.Line[j] + for k := 0; k < len(li.Token) && k < len(lj.Token); k++ { + if li.Token[k] != lj.Token[k] { + return li.Token[k] < lj.Token[k] + } + } + return len(li.Token) < len(lj.Token) + }) + } +} + +func (f *File) removeDups() { + have := make(map[module.Version]bool) + kill := make(map[*Line]bool) + for _, x := range f.Exclude { + if have[x.Mod] { + kill[x.Syntax] = true + continue + } + have[x.Mod] = true + } + var excl []*Exclude + for _, x := range f.Exclude { + if !kill[x.Syntax] { + excl = append(excl, x) + } + } + f.Exclude = excl + + have = make(map[module.Version]bool) + // Later replacements take priority over earlier ones. + for i := len(f.Replace) - 1; i >= 0; i-- { + x := f.Replace[i] + if have[x.Old] { + kill[x.Syntax] = true + continue + } + have[x.Old] = true + } + var repl []*Replace + for _, x := range f.Replace { + if !kill[x.Syntax] { + repl = append(repl, x) + } + } + f.Replace = repl + + var stmts []Expr + for _, stmt := range f.Syntax.Stmt { + switch stmt := stmt.(type) { + case *Line: + if kill[stmt] { + continue + } + case *LineBlock: + var lines []*Line + for _, line := range stmt.Line { + if !kill[line] { + lines = append(lines, line) + } + } + stmt.Line = lines + if len(lines) == 0 { + continue + } + } + stmts = append(stmts, stmt) + } + f.Syntax.Stmt = stmts +} diff --git a/vendor/github.com/rogpeppe/go-internal/module/BUILD.bazel b/vendor/github.com/rogpeppe/go-internal/module/BUILD.bazel new file mode 100644 index 00000000000..d2e8b32f451 --- /dev/null +++ b/vendor/github.com/rogpeppe/go-internal/module/BUILD.bazel @@ -0,0 +1,10 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["module.go"], + importmap = "sigs.k8s.io/cluster-api-provider-azure/vendor/github.com/rogpeppe/go-internal/module", + importpath = "github.com/rogpeppe/go-internal/module", + visibility = ["//visibility:public"], + deps = ["//vendor/github.com/rogpeppe/go-internal/semver:go_default_library"], +) diff --git a/vendor/github.com/rogpeppe/go-internal/module/module.go b/vendor/github.com/rogpeppe/go-internal/module/module.go new file mode 100644 index 00000000000..3ff6d9bf535 --- /dev/null +++ b/vendor/github.com/rogpeppe/go-internal/module/module.go @@ -0,0 +1,540 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package module defines the module.Version type +// along with support code. +package module + +// IMPORTANT NOTE +// +// This file essentially defines the set of valid import paths for the go command. +// There are many subtle considerations, including Unicode ambiguity, +// security, network, and file system representations. +// +// This file also defines the set of valid module path and version combinations, +// another topic with many subtle considerations. +// +// Changes to the semantics in this file require approval from rsc. + +import ( + "fmt" + "sort" + "strings" + "unicode" + "unicode/utf8" + + "github.com/rogpeppe/go-internal/semver" +) + +// A Version is defined by a module path and version pair. +type Version struct { + Path string + + // Version is usually a semantic version in canonical form. + // There are two exceptions to this general rule. + // First, the top-level target of a build has no specific version + // and uses Version = "". + // Second, during MVS calculations the version "none" is used + // to represent the decision to take no version of a given module. + Version string `json:",omitempty"` +} + +// Check checks that a given module path, version pair is valid. +// In addition to the path being a valid module path +// and the version being a valid semantic version, +// the two must correspond. +// For example, the path "yaml/v2" only corresponds to +// semantic versions beginning with "v2.". +func Check(path, version string) error { + if err := CheckPath(path); err != nil { + return err + } + if !semver.IsValid(version) { + return fmt.Errorf("malformed semantic version %v", version) + } + _, pathMajor, _ := SplitPathVersion(path) + if !MatchPathMajor(version, pathMajor) { + if pathMajor == "" { + pathMajor = "v0 or v1" + } + if pathMajor[0] == '.' { // .v1 + pathMajor = pathMajor[1:] + } + return fmt.Errorf("mismatched module path %v and version %v (want %v)", path, version, pathMajor) + } + return nil +} + +// firstPathOK reports whether r can appear in the first element of a module path. +// The first element of the path must be an LDH domain name, at least for now. +// To avoid case ambiguity, the domain name must be entirely lower case. +func firstPathOK(r rune) bool { + return r == '-' || r == '.' || + '0' <= r && r <= '9' || + 'a' <= r && r <= 'z' +} + +// pathOK reports whether r can appear in an import path element. +// Paths can be ASCII letters, ASCII digits, and limited ASCII punctuation: + - . _ and ~. +// This matches what "go get" has historically recognized in import paths. +// TODO(rsc): We would like to allow Unicode letters, but that requires additional +// care in the safe encoding (see note below). +func pathOK(r rune) bool { + if r < utf8.RuneSelf { + return r == '+' || r == '-' || r == '.' || r == '_' || r == '~' || + '0' <= r && r <= '9' || + 'A' <= r && r <= 'Z' || + 'a' <= r && r <= 'z' + } + return false +} + +// fileNameOK reports whether r can appear in a file name. +// For now we allow all Unicode letters but otherwise limit to pathOK plus a few more punctuation characters. +// If we expand the set of allowed characters here, we have to +// work harder at detecting potential case-folding and normalization collisions. +// See note about "safe encoding" below. +func fileNameOK(r rune) bool { + if r < utf8.RuneSelf { + // Entire set of ASCII punctuation, from which we remove characters: + // ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~ + // We disallow some shell special characters: " ' * < > ? ` | + // (Note that some of those are disallowed by the Windows file system as well.) + // We also disallow path separators / : and \ (fileNameOK is only called on path element characters). + // We allow spaces (U+0020) in file names. + const allowed = "!#$%&()+,-.=@[]^_{}~ " + if '0' <= r && r <= '9' || 'A' <= r && r <= 'Z' || 'a' <= r && r <= 'z' { + return true + } + for i := 0; i < len(allowed); i++ { + if rune(allowed[i]) == r { + return true + } + } + return false + } + // It may be OK to add more ASCII punctuation here, but only carefully. + // For example Windows disallows < > \, and macOS disallows :, so we must not allow those. + return unicode.IsLetter(r) +} + +// CheckPath checks that a module path is valid. +func CheckPath(path string) error { + if err := checkPath(path, false); err != nil { + return fmt.Errorf("malformed module path %q: %v", path, err) + } + i := strings.Index(path, "/") + if i < 0 { + i = len(path) + } + if i == 0 { + return fmt.Errorf("malformed module path %q: leading slash", path) + } + if !strings.Contains(path[:i], ".") { + return fmt.Errorf("malformed module path %q: missing dot in first path element", path) + } + if path[0] == '-' { + return fmt.Errorf("malformed module path %q: leading dash in first path element", path) + } + for _, r := range path[:i] { + if !firstPathOK(r) { + return fmt.Errorf("malformed module path %q: invalid char %q in first path element", path, r) + } + } + if _, _, ok := SplitPathVersion(path); !ok { + return fmt.Errorf("malformed module path %q: invalid version", path) + } + return nil +} + +// CheckImportPath checks that an import path is valid. +func CheckImportPath(path string) error { + if err := checkPath(path, false); err != nil { + return fmt.Errorf("malformed import path %q: %v", path, err) + } + return nil +} + +// checkPath checks that a general path is valid. +// It returns an error describing why but not mentioning path. +// Because these checks apply to both module paths and import paths, +// the caller is expected to add the "malformed ___ path %q: " prefix. +// fileName indicates whether the final element of the path is a file name +// (as opposed to a directory name). +func checkPath(path string, fileName bool) error { + if !utf8.ValidString(path) { + return fmt.Errorf("invalid UTF-8") + } + if path == "" { + return fmt.Errorf("empty string") + } + if strings.Contains(path, "..") { + return fmt.Errorf("double dot") + } + if strings.Contains(path, "//") { + return fmt.Errorf("double slash") + } + if path[len(path)-1] == '/' { + return fmt.Errorf("trailing slash") + } + elemStart := 0 + for i, r := range path { + if r == '/' { + if err := checkElem(path[elemStart:i], fileName); err != nil { + return err + } + elemStart = i + 1 + } + } + if err := checkElem(path[elemStart:], fileName); err != nil { + return err + } + return nil +} + +// checkElem checks whether an individual path element is valid. +// fileName indicates whether the element is a file name (not a directory name). +func checkElem(elem string, fileName bool) error { + if elem == "" { + return fmt.Errorf("empty path element") + } + if strings.Count(elem, ".") == len(elem) { + return fmt.Errorf("invalid path element %q", elem) + } + if elem[0] == '.' && !fileName { + return fmt.Errorf("leading dot in path element") + } + if elem[len(elem)-1] == '.' { + return fmt.Errorf("trailing dot in path element") + } + charOK := pathOK + if fileName { + charOK = fileNameOK + } + for _, r := range elem { + if !charOK(r) { + return fmt.Errorf("invalid char %q", r) + } + } + + // Windows disallows a bunch of path elements, sadly. + // See https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file + short := elem + if i := strings.Index(short, "."); i >= 0 { + short = short[:i] + } + for _, bad := range badWindowsNames { + if strings.EqualFold(bad, short) { + return fmt.Errorf("disallowed path element %q", elem) + } + } + return nil +} + +// CheckFilePath checks whether a slash-separated file path is valid. +func CheckFilePath(path string) error { + if err := checkPath(path, true); err != nil { + return fmt.Errorf("malformed file path %q: %v", path, err) + } + return nil +} + +// badWindowsNames are the reserved file path elements on Windows. +// See https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file +var badWindowsNames = []string{ + "CON", + "PRN", + "AUX", + "NUL", + "COM1", + "COM2", + "COM3", + "COM4", + "COM5", + "COM6", + "COM7", + "COM8", + "COM9", + "LPT1", + "LPT2", + "LPT3", + "LPT4", + "LPT5", + "LPT6", + "LPT7", + "LPT8", + "LPT9", +} + +// SplitPathVersion returns prefix and major version such that prefix+pathMajor == path +// and version is either empty or "/vN" for N >= 2. +// As a special case, gopkg.in paths are recognized directly; +// they require ".vN" instead of "/vN", and for all N, not just N >= 2. +func SplitPathVersion(path string) (prefix, pathMajor string, ok bool) { + if strings.HasPrefix(path, "gopkg.in/") { + return splitGopkgIn(path) + } + + i := len(path) + dot := false + for i > 0 && ('0' <= path[i-1] && path[i-1] <= '9' || path[i-1] == '.') { + if path[i-1] == '.' { + dot = true + } + i-- + } + if i <= 1 || path[i-1] != 'v' || path[i-2] != '/' { + return path, "", true + } + prefix, pathMajor = path[:i-2], path[i-2:] + if dot || len(pathMajor) <= 2 || pathMajor[2] == '0' || pathMajor == "/v1" { + return path, "", false + } + return prefix, pathMajor, true +} + +// splitGopkgIn is like SplitPathVersion but only for gopkg.in paths. +func splitGopkgIn(path string) (prefix, pathMajor string, ok bool) { + if !strings.HasPrefix(path, "gopkg.in/") { + return path, "", false + } + i := len(path) + if strings.HasSuffix(path, "-unstable") { + i -= len("-unstable") + } + for i > 0 && ('0' <= path[i-1] && path[i-1] <= '9') { + i-- + } + if i <= 1 || path[i-1] != 'v' || path[i-2] != '.' { + // All gopkg.in paths must end in vN for some N. + return path, "", false + } + prefix, pathMajor = path[:i-2], path[i-2:] + if len(pathMajor) <= 2 || pathMajor[2] == '0' && pathMajor != ".v0" { + return path, "", false + } + return prefix, pathMajor, true +} + +// MatchPathMajor reports whether the semantic version v +// matches the path major version pathMajor. +func MatchPathMajor(v, pathMajor string) bool { + if strings.HasPrefix(pathMajor, ".v") && strings.HasSuffix(pathMajor, "-unstable") { + pathMajor = strings.TrimSuffix(pathMajor, "-unstable") + } + if strings.HasPrefix(v, "v0.0.0-") && pathMajor == ".v1" { + // Allow old bug in pseudo-versions that generated v0.0.0- pseudoversion for gopkg .v1. + // For example, gopkg.in/yaml.v2@v2.2.1's go.mod requires gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405. + return true + } + m := semver.Major(v) + if pathMajor == "" { + return m == "v0" || m == "v1" || semver.Build(v) == "+incompatible" + } + return (pathMajor[0] == '/' || pathMajor[0] == '.') && m == pathMajor[1:] +} + +// CanonicalVersion returns the canonical form of the version string v. +// It is the same as semver.Canonical(v) except that it preserves the special build suffix "+incompatible". +func CanonicalVersion(v string) string { + cv := semver.Canonical(v) + if semver.Build(v) == "+incompatible" { + cv += "+incompatible" + } + return cv +} + +// Sort sorts the list by Path, breaking ties by comparing Versions. +func Sort(list []Version) { + sort.Slice(list, func(i, j int) bool { + mi := list[i] + mj := list[j] + if mi.Path != mj.Path { + return mi.Path < mj.Path + } + // To help go.sum formatting, allow version/file. + // Compare semver prefix by semver rules, + // file by string order. + vi := mi.Version + vj := mj.Version + var fi, fj string + if k := strings.Index(vi, "/"); k >= 0 { + vi, fi = vi[:k], vi[k:] + } + if k := strings.Index(vj, "/"); k >= 0 { + vj, fj = vj[:k], vj[k:] + } + if vi != vj { + return semver.Compare(vi, vj) < 0 + } + return fi < fj + }) +} + +// Safe encodings +// +// Module paths appear as substrings of file system paths +// (in the download cache) and of web server URLs in the proxy protocol. +// In general we cannot rely on file systems to be case-sensitive, +// nor can we rely on web servers, since they read from file systems. +// That is, we cannot rely on the file system to keep rsc.io/QUOTE +// and rsc.io/quote separate. Windows and macOS don't. +// Instead, we must never require two different casings of a file path. +// Because we want the download cache to match the proxy protocol, +// and because we want the proxy protocol to be possible to serve +// from a tree of static files (which might be stored on a case-insensitive +// file system), the proxy protocol must never require two different casings +// of a URL path either. +// +// One possibility would be to make the safe encoding be the lowercase +// hexadecimal encoding of the actual path bytes. This would avoid ever +// needing different casings of a file path, but it would be fairly illegible +// to most programmers when those paths appeared in the file system +// (including in file paths in compiler errors and stack traces) +// in web server logs, and so on. Instead, we want a safe encoding that +// leaves most paths unaltered. +// +// The safe encoding is this: +// replace every uppercase letter with an exclamation mark +// followed by the letter's lowercase equivalent. +// +// For example, +// github.com/Azure/azure-sdk-for-go -> github.com/!azure/azure-sdk-for-go. +// github.com/GoogleCloudPlatform/cloudsql-proxy -> github.com/!google!cloud!platform/cloudsql-proxy +// github.com/Sirupsen/logrus -> github.com/!sirupsen/logrus. +// +// Import paths that avoid upper-case letters are left unchanged. +// Note that because import paths are ASCII-only and avoid various +// problematic punctuation (like : < and >), the safe encoding is also ASCII-only +// and avoids the same problematic punctuation. +// +// Import paths have never allowed exclamation marks, so there is no +// need to define how to encode a literal !. +// +// Although paths are disallowed from using Unicode (see pathOK above), +// the eventual plan is to allow Unicode letters as well, to assume that +// file systems and URLs are Unicode-safe (storing UTF-8), and apply +// the !-for-uppercase convention. Note however that not all runes that +// are different but case-fold equivalent are an upper/lower pair. +// For example, U+004B ('K'), U+006B ('k'), and U+212A ('K' for Kelvin) +// are considered to case-fold to each other. When we do add Unicode +// letters, we must not assume that upper/lower are the only case-equivalent pairs. +// Perhaps the Kelvin symbol would be disallowed entirely, for example. +// Or perhaps it would encode as "!!k", or perhaps as "(212A)". +// +// Also, it would be nice to allow Unicode marks as well as letters, +// but marks include combining marks, and then we must deal not +// only with case folding but also normalization: both U+00E9 ('é') +// and U+0065 U+0301 ('e' followed by combining acute accent) +// look the same on the page and are treated by some file systems +// as the same path. If we do allow Unicode marks in paths, there +// must be some kind of normalization to allow only one canonical +// encoding of any character used in an import path. + +// EncodePath returns the safe encoding of the given module path. +// It fails if the module path is invalid. +func EncodePath(path string) (encoding string, err error) { + if err := CheckPath(path); err != nil { + return "", err + } + + return encodeString(path) +} + +// EncodeVersion returns the safe encoding of the given module version. +// Versions are allowed to be in non-semver form but must be valid file names +// and not contain exclamation marks. +func EncodeVersion(v string) (encoding string, err error) { + if err := checkElem(v, true); err != nil || strings.Contains(v, "!") { + return "", fmt.Errorf("disallowed version string %q", v) + } + return encodeString(v) +} + +func encodeString(s string) (encoding string, err error) { + haveUpper := false + for _, r := range s { + if r == '!' || r >= utf8.RuneSelf { + // This should be disallowed by CheckPath, but diagnose anyway. + // The correctness of the encoding loop below depends on it. + return "", fmt.Errorf("internal error: inconsistency in EncodePath") + } + if 'A' <= r && r <= 'Z' { + haveUpper = true + } + } + + if !haveUpper { + return s, nil + } + + var buf []byte + for _, r := range s { + if 'A' <= r && r <= 'Z' { + buf = append(buf, '!', byte(r+'a'-'A')) + } else { + buf = append(buf, byte(r)) + } + } + return string(buf), nil +} + +// DecodePath returns the module path of the given safe encoding. +// It fails if the encoding is invalid or encodes an invalid path. +func DecodePath(encoding string) (path string, err error) { + path, ok := decodeString(encoding) + if !ok { + return "", fmt.Errorf("invalid module path encoding %q", encoding) + } + if err := CheckPath(path); err != nil { + return "", fmt.Errorf("invalid module path encoding %q: %v", encoding, err) + } + return path, nil +} + +// DecodeVersion returns the version string for the given safe encoding. +// It fails if the encoding is invalid or encodes an invalid version. +// Versions are allowed to be in non-semver form but must be valid file names +// and not contain exclamation marks. +func DecodeVersion(encoding string) (v string, err error) { + v, ok := decodeString(encoding) + if !ok { + return "", fmt.Errorf("invalid version encoding %q", encoding) + } + if err := checkElem(v, true); err != nil { + return "", fmt.Errorf("disallowed version string %q", v) + } + return v, nil +} + +func decodeString(encoding string) (string, bool) { + var buf []byte + + bang := false + for _, r := range encoding { + if r >= utf8.RuneSelf { + return "", false + } + if bang { + bang = false + if r < 'a' || 'z' < r { + return "", false + } + buf = append(buf, byte(r+'A'-'a')) + continue + } + if r == '!' { + bang = true + continue + } + if 'A' <= r && r <= 'Z' { + return "", false + } + buf = append(buf, byte(r)) + } + if bang { + return "", false + } + return string(buf), true +} diff --git a/vendor/github.com/kr/fs/BUILD.bazel b/vendor/github.com/rogpeppe/go-internal/semver/BUILD.bazel similarity index 62% rename from vendor/github.com/kr/fs/BUILD.bazel rename to vendor/github.com/rogpeppe/go-internal/semver/BUILD.bazel index 4f4e2579baf..b576315107d 100644 --- a/vendor/github.com/kr/fs/BUILD.bazel +++ b/vendor/github.com/rogpeppe/go-internal/semver/BUILD.bazel @@ -2,11 +2,8 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "go_default_library", - srcs = [ - "filesystem.go", - "walk.go", - ], - importmap = "sigs.k8s.io/cluster-api-provider-azure/vendor/github.com/kr/fs", - importpath = "github.com/kr/fs", + srcs = ["semver.go"], + importmap = "sigs.k8s.io/cluster-api-provider-azure/vendor/github.com/rogpeppe/go-internal/semver", + importpath = "github.com/rogpeppe/go-internal/semver", visibility = ["//visibility:public"], ) diff --git a/vendor/github.com/rogpeppe/go-internal/semver/semver.go b/vendor/github.com/rogpeppe/go-internal/semver/semver.go new file mode 100644 index 00000000000..4af7118e55d --- /dev/null +++ b/vendor/github.com/rogpeppe/go-internal/semver/semver.go @@ -0,0 +1,388 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package semver implements comparison of semantic version strings. +// In this package, semantic version strings must begin with a leading "v", +// as in "v1.0.0". +// +// The general form of a semantic version string accepted by this package is +// +// vMAJOR[.MINOR[.PATCH[-PRERELEASE][+BUILD]]] +// +// where square brackets indicate optional parts of the syntax; +// MAJOR, MINOR, and PATCH are decimal integers without extra leading zeros; +// PRERELEASE and BUILD are each a series of non-empty dot-separated identifiers +// using only alphanumeric characters and hyphens; and +// all-numeric PRERELEASE identifiers must not have leading zeros. +// +// This package follows Semantic Versioning 2.0.0 (see semver.org) +// with two exceptions. First, it requires the "v" prefix. Second, it recognizes +// vMAJOR and vMAJOR.MINOR (with no prerelease or build suffixes) +// as shorthands for vMAJOR.0.0 and vMAJOR.MINOR.0. +package semver + +// parsed returns the parsed form of a semantic version string. +type parsed struct { + major string + minor string + patch string + short string + prerelease string + build string + err string +} + +// IsValid reports whether v is a valid semantic version string. +func IsValid(v string) bool { + _, ok := parse(v) + return ok +} + +// Canonical returns the canonical formatting of the semantic version v. +// It fills in any missing .MINOR or .PATCH and discards build metadata. +// Two semantic versions compare equal only if their canonical formattings +// are identical strings. +// The canonical invalid semantic version is the empty string. +func Canonical(v string) string { + p, ok := parse(v) + if !ok { + return "" + } + if p.build != "" { + return v[:len(v)-len(p.build)] + } + if p.short != "" { + return v + p.short + } + return v +} + +// Major returns the major version prefix of the semantic version v. +// For example, Major("v2.1.0") == "v2". +// If v is an invalid semantic version string, Major returns the empty string. +func Major(v string) string { + pv, ok := parse(v) + if !ok { + return "" + } + return v[:1+len(pv.major)] +} + +// MajorMinor returns the major.minor version prefix of the semantic version v. +// For example, MajorMinor("v2.1.0") == "v2.1". +// If v is an invalid semantic version string, MajorMinor returns the empty string. +func MajorMinor(v string) string { + pv, ok := parse(v) + if !ok { + return "" + } + i := 1 + len(pv.major) + if j := i + 1 + len(pv.minor); j <= len(v) && v[i] == '.' && v[i+1:j] == pv.minor { + return v[:j] + } + return v[:i] + "." + pv.minor +} + +// Prerelease returns the prerelease suffix of the semantic version v. +// For example, Prerelease("v2.1.0-pre+meta") == "-pre". +// If v is an invalid semantic version string, Prerelease returns the empty string. +func Prerelease(v string) string { + pv, ok := parse(v) + if !ok { + return "" + } + return pv.prerelease +} + +// Build returns the build suffix of the semantic version v. +// For example, Build("v2.1.0+meta") == "+meta". +// If v is an invalid semantic version string, Build returns the empty string. +func Build(v string) string { + pv, ok := parse(v) + if !ok { + return "" + } + return pv.build +} + +// Compare returns an integer comparing two versions according to +// according to semantic version precedence. +// The result will be 0 if v == w, -1 if v < w, or +1 if v > w. +// +// An invalid semantic version string is considered less than a valid one. +// All invalid semantic version strings compare equal to each other. +func Compare(v, w string) int { + pv, ok1 := parse(v) + pw, ok2 := parse(w) + if !ok1 && !ok2 { + return 0 + } + if !ok1 { + return -1 + } + if !ok2 { + return +1 + } + if c := compareInt(pv.major, pw.major); c != 0 { + return c + } + if c := compareInt(pv.minor, pw.minor); c != 0 { + return c + } + if c := compareInt(pv.patch, pw.patch); c != 0 { + return c + } + return comparePrerelease(pv.prerelease, pw.prerelease) +} + +// Max canonicalizes its arguments and then returns the version string +// that compares greater. +func Max(v, w string) string { + v = Canonical(v) + w = Canonical(w) + if Compare(v, w) > 0 { + return v + } + return w +} + +func parse(v string) (p parsed, ok bool) { + if v == "" || v[0] != 'v' { + p.err = "missing v prefix" + return + } + p.major, v, ok = parseInt(v[1:]) + if !ok { + p.err = "bad major version" + return + } + if v == "" { + p.minor = "0" + p.patch = "0" + p.short = ".0.0" + return + } + if v[0] != '.' { + p.err = "bad minor prefix" + ok = false + return + } + p.minor, v, ok = parseInt(v[1:]) + if !ok { + p.err = "bad minor version" + return + } + if v == "" { + p.patch = "0" + p.short = ".0" + return + } + if v[0] != '.' { + p.err = "bad patch prefix" + ok = false + return + } + p.patch, v, ok = parseInt(v[1:]) + if !ok { + p.err = "bad patch version" + return + } + if len(v) > 0 && v[0] == '-' { + p.prerelease, v, ok = parsePrerelease(v) + if !ok { + p.err = "bad prerelease" + return + } + } + if len(v) > 0 && v[0] == '+' { + p.build, v, ok = parseBuild(v) + if !ok { + p.err = "bad build" + return + } + } + if v != "" { + p.err = "junk on end" + ok = false + return + } + ok = true + return +} + +func parseInt(v string) (t, rest string, ok bool) { + if v == "" { + return + } + if v[0] < '0' || '9' < v[0] { + return + } + i := 1 + for i < len(v) && '0' <= v[i] && v[i] <= '9' { + i++ + } + if v[0] == '0' && i != 1 { + return + } + return v[:i], v[i:], true +} + +func parsePrerelease(v string) (t, rest string, ok bool) { + // "A pre-release version MAY be denoted by appending a hyphen and + // a series of dot separated identifiers immediately following the patch version. + // Identifiers MUST comprise only ASCII alphanumerics and hyphen [0-9A-Za-z-]. + // Identifiers MUST NOT be empty. Numeric identifiers MUST NOT include leading zeroes." + if v == "" || v[0] != '-' { + return + } + i := 1 + start := 1 + for i < len(v) && v[i] != '+' { + if !isIdentChar(v[i]) && v[i] != '.' { + return + } + if v[i] == '.' { + if start == i || isBadNum(v[start:i]) { + return + } + start = i + 1 + } + i++ + } + if start == i || isBadNum(v[start:i]) { + return + } + return v[:i], v[i:], true +} + +func parseBuild(v string) (t, rest string, ok bool) { + if v == "" || v[0] != '+' { + return + } + i := 1 + start := 1 + for i < len(v) { + if !isIdentChar(v[i]) { + return + } + if v[i] == '.' { + if start == i { + return + } + start = i + 1 + } + i++ + } + if start == i { + return + } + return v[:i], v[i:], true +} + +func isIdentChar(c byte) bool { + return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '-' +} + +func isBadNum(v string) bool { + i := 0 + for i < len(v) && '0' <= v[i] && v[i] <= '9' { + i++ + } + return i == len(v) && i > 1 && v[0] == '0' +} + +func isNum(v string) bool { + i := 0 + for i < len(v) && '0' <= v[i] && v[i] <= '9' { + i++ + } + return i == len(v) +} + +func compareInt(x, y string) int { + if x == y { + return 0 + } + if len(x) < len(y) { + return -1 + } + if len(x) > len(y) { + return +1 + } + if x < y { + return -1 + } else { + return +1 + } +} + +func comparePrerelease(x, y string) int { + // "When major, minor, and patch are equal, a pre-release version has + // lower precedence than a normal version. + // Example: 1.0.0-alpha < 1.0.0. + // Precedence for two pre-release versions with the same major, minor, + // and patch version MUST be determined by comparing each dot separated + // identifier from left to right until a difference is found as follows: + // identifiers consisting of only digits are compared numerically and + // identifiers with letters or hyphens are compared lexically in ASCII + // sort order. Numeric identifiers always have lower precedence than + // non-numeric identifiers. A larger set of pre-release fields has a + // higher precedence than a smaller set, if all of the preceding + // identifiers are equal. + // Example: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < + // 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0." + if x == y { + return 0 + } + if x == "" { + return +1 + } + if y == "" { + return -1 + } + for x != "" && y != "" { + x = x[1:] // skip - or . + y = y[1:] // skip - or . + var dx, dy string + dx, x = nextIdent(x) + dy, y = nextIdent(y) + if dx != dy { + ix := isNum(dx) + iy := isNum(dy) + if ix != iy { + if ix { + return -1 + } else { + return +1 + } + } + if ix { + if len(dx) < len(dy) { + return -1 + } + if len(dx) > len(dy) { + return +1 + } + } + if dx < dy { + return -1 + } else { + return +1 + } + } + } + if x == "" { + return -1 + } else { + return +1 + } +} + +func nextIdent(x string) (dx, rest string) { + i := 0 + for i < len(x) && x[i] != '.' { + i++ + } + return x[:i], x[i:] +} diff --git a/vendor/github.com/spf13/afero/copyOnWriteFs.go b/vendor/github.com/spf13/afero/copyOnWriteFs.go index 9aef39794c1..e8108a851e1 100644 --- a/vendor/github.com/spf13/afero/copyOnWriteFs.go +++ b/vendor/github.com/spf13/afero/copyOnWriteFs.go @@ -267,7 +267,7 @@ func (u *CopyOnWriteFs) Mkdir(name string, perm os.FileMode) error { return u.layer.MkdirAll(name, perm) } if dir { - return syscall.EEXIST + return ErrFileExists } return u.layer.MkdirAll(name, perm) } @@ -282,7 +282,8 @@ func (u *CopyOnWriteFs) MkdirAll(name string, perm os.FileMode) error { return u.layer.MkdirAll(name, perm) } if dir { - return syscall.EEXIST + // This is in line with how os.MkdirAll behaves. + return nil } return u.layer.MkdirAll(name, perm) } diff --git a/vendor/github.com/spf13/afero/go.mod b/vendor/github.com/spf13/afero/go.mod index 9eff4fed5b8..08685509957 100644 --- a/vendor/github.com/spf13/afero/go.mod +++ b/vendor/github.com/spf13/afero/go.mod @@ -1 +1,3 @@ module github.com/spf13/afero + +require golang.org/x/text v0.3.0 diff --git a/vendor/github.com/spf13/afero/go.sum b/vendor/github.com/spf13/afero/go.sum new file mode 100644 index 00000000000..6bad37b2a77 --- /dev/null +++ b/vendor/github.com/spf13/afero/go.sum @@ -0,0 +1,2 @@ +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/vendor/github.com/spf13/afero/unionFile.go b/vendor/github.com/spf13/afero/unionFile.go index 1e78f7d1efb..abcf12d36d6 100644 --- a/vendor/github.com/spf13/afero/unionFile.go +++ b/vendor/github.com/spf13/afero/unionFile.go @@ -156,6 +156,7 @@ var defaultUnionMergeDirsFn = func(lofi, bofi []os.FileInfo) ([]os.FileInfo, err // Readdir will weave the two directories together and // return a single view of the overlayed directories +// At the end of the directory view, the error is io.EOF. func (f *UnionFile) Readdir(c int) (ofi []os.FileInfo, err error) { var merge DirsMerger = f.Merger if merge == nil { @@ -185,9 +186,19 @@ func (f *UnionFile) Readdir(c int) (ofi []os.FileInfo, err error) { } f.files = append(f.files, merged...) } + + if f.off >= len(f.files) { + return nil, io.EOF + } + if c == -1 { return f.files[f.off:], nil } + + if c > len(f.files) { + c = len(f.files) + } + defer func() { f.off += c }() return f.files[f.off:c], nil } diff --git a/vendor/golang.org/x/crypto/curve25519/curve25519.go b/vendor/golang.org/x/crypto/curve25519/curve25519.go index cb8fbc57b97..75f24babb69 100644 --- a/vendor/golang.org/x/crypto/curve25519/curve25519.go +++ b/vendor/golang.org/x/crypto/curve25519/curve25519.go @@ -86,7 +86,7 @@ func feFromBytes(dst *fieldElement, src *[32]byte) { h6 := load3(src[20:]) << 7 h7 := load3(src[23:]) << 5 h8 := load3(src[26:]) << 4 - h9 := load3(src[29:]) << 2 + h9 := (load3(src[29:]) & 0x7fffff) << 2 var carry [10]int64 carry[9] = (h9 + 1<<24) >> 25 diff --git a/vendor/golang.org/x/crypto/internal/chacha20/BUILD.bazel b/vendor/golang.org/x/crypto/internal/chacha20/BUILD.bazel index 5bf3744fd05..904068ed399 100644 --- a/vendor/golang.org/x/crypto/internal/chacha20/BUILD.bazel +++ b/vendor/golang.org/x/crypto/internal/chacha20/BUILD.bazel @@ -3,6 +3,8 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "go_default_library", srcs = [ + "asm_arm64.s", + "chacha_arm64.go", "chacha_generic.go", "chacha_noasm.go", "chacha_s390x.go", diff --git a/vendor/golang.org/x/crypto/internal/chacha20/asm_arm64.s b/vendor/golang.org/x/crypto/internal/chacha20/asm_arm64.s new file mode 100644 index 00000000000..b3a16ef751a --- /dev/null +++ b/vendor/golang.org/x/crypto/internal/chacha20/asm_arm64.s @@ -0,0 +1,308 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build go1.11 +// +build !gccgo,!appengine + +#include "textflag.h" + +#define NUM_ROUNDS 10 + +// func xorKeyStreamVX(dst, src []byte, key *[8]uint32, nonce *[3]uint32, counter *uint32) +TEXT ·xorKeyStreamVX(SB), NOSPLIT, $0 + MOVD dst+0(FP), R1 + MOVD src+24(FP), R2 + MOVD src_len+32(FP), R3 + MOVD key+48(FP), R4 + MOVD nonce+56(FP), R6 + MOVD counter+64(FP), R7 + + MOVD $·constants(SB), R10 + MOVD $·incRotMatrix(SB), R11 + + MOVW (R7), R20 + + AND $~255, R3, R13 + ADD R2, R13, R12 // R12 for block end + AND $255, R3, R13 +loop: + MOVD $NUM_ROUNDS, R21 + VLD1 (R11), [V30.S4, V31.S4] + + // load contants + // VLD4R (R10), [V0.S4, V1.S4, V2.S4, V3.S4] + WORD $0x4D60E940 + + // load keys + // VLD4R 16(R4), [V4.S4, V5.S4, V6.S4, V7.S4] + WORD $0x4DFFE884 + // VLD4R 16(R4), [V8.S4, V9.S4, V10.S4, V11.S4] + WORD $0x4DFFE888 + SUB $32, R4 + + // load counter + nonce + // VLD1R (R7), [V12.S4] + WORD $0x4D40C8EC + + // VLD3R (R6), [V13.S4, V14.S4, V15.S4] + WORD $0x4D40E8CD + + // update counter + VADD V30.S4, V12.S4, V12.S4 + +chacha: + // V0..V3 += V4..V7 + // V12..V15 <<<= ((V12..V15 XOR V0..V3), 16) + VADD V0.S4, V4.S4, V0.S4 + VADD V1.S4, V5.S4, V1.S4 + VADD V2.S4, V6.S4, V2.S4 + VADD V3.S4, V7.S4, V3.S4 + VEOR V12.B16, V0.B16, V12.B16 + VEOR V13.B16, V1.B16, V13.B16 + VEOR V14.B16, V2.B16, V14.B16 + VEOR V15.B16, V3.B16, V15.B16 + VREV32 V12.H8, V12.H8 + VREV32 V13.H8, V13.H8 + VREV32 V14.H8, V14.H8 + VREV32 V15.H8, V15.H8 + // V8..V11 += V12..V15 + // V4..V7 <<<= ((V4..V7 XOR V8..V11), 12) + VADD V8.S4, V12.S4, V8.S4 + VADD V9.S4, V13.S4, V9.S4 + VADD V10.S4, V14.S4, V10.S4 + VADD V11.S4, V15.S4, V11.S4 + VEOR V8.B16, V4.B16, V16.B16 + VEOR V9.B16, V5.B16, V17.B16 + VEOR V10.B16, V6.B16, V18.B16 + VEOR V11.B16, V7.B16, V19.B16 + VSHL $12, V16.S4, V4.S4 + VSHL $12, V17.S4, V5.S4 + VSHL $12, V18.S4, V6.S4 + VSHL $12, V19.S4, V7.S4 + VSRI $20, V16.S4, V4.S4 + VSRI $20, V17.S4, V5.S4 + VSRI $20, V18.S4, V6.S4 + VSRI $20, V19.S4, V7.S4 + + // V0..V3 += V4..V7 + // V12..V15 <<<= ((V12..V15 XOR V0..V3), 8) + VADD V0.S4, V4.S4, V0.S4 + VADD V1.S4, V5.S4, V1.S4 + VADD V2.S4, V6.S4, V2.S4 + VADD V3.S4, V7.S4, V3.S4 + VEOR V12.B16, V0.B16, V12.B16 + VEOR V13.B16, V1.B16, V13.B16 + VEOR V14.B16, V2.B16, V14.B16 + VEOR V15.B16, V3.B16, V15.B16 + VTBL V31.B16, [V12.B16], V12.B16 + VTBL V31.B16, [V13.B16], V13.B16 + VTBL V31.B16, [V14.B16], V14.B16 + VTBL V31.B16, [V15.B16], V15.B16 + + // V8..V11 += V12..V15 + // V4..V7 <<<= ((V4..V7 XOR V8..V11), 7) + VADD V12.S4, V8.S4, V8.S4 + VADD V13.S4, V9.S4, V9.S4 + VADD V14.S4, V10.S4, V10.S4 + VADD V15.S4, V11.S4, V11.S4 + VEOR V8.B16, V4.B16, V16.B16 + VEOR V9.B16, V5.B16, V17.B16 + VEOR V10.B16, V6.B16, V18.B16 + VEOR V11.B16, V7.B16, V19.B16 + VSHL $7, V16.S4, V4.S4 + VSHL $7, V17.S4, V5.S4 + VSHL $7, V18.S4, V6.S4 + VSHL $7, V19.S4, V7.S4 + VSRI $25, V16.S4, V4.S4 + VSRI $25, V17.S4, V5.S4 + VSRI $25, V18.S4, V6.S4 + VSRI $25, V19.S4, V7.S4 + + // V0..V3 += V5..V7, V4 + // V15,V12-V14 <<<= ((V15,V12-V14 XOR V0..V3), 16) + VADD V0.S4, V5.S4, V0.S4 + VADD V1.S4, V6.S4, V1.S4 + VADD V2.S4, V7.S4, V2.S4 + VADD V3.S4, V4.S4, V3.S4 + VEOR V15.B16, V0.B16, V15.B16 + VEOR V12.B16, V1.B16, V12.B16 + VEOR V13.B16, V2.B16, V13.B16 + VEOR V14.B16, V3.B16, V14.B16 + VREV32 V12.H8, V12.H8 + VREV32 V13.H8, V13.H8 + VREV32 V14.H8, V14.H8 + VREV32 V15.H8, V15.H8 + + // V10 += V15; V5 <<<= ((V10 XOR V5), 12) + // ... + VADD V15.S4, V10.S4, V10.S4 + VADD V12.S4, V11.S4, V11.S4 + VADD V13.S4, V8.S4, V8.S4 + VADD V14.S4, V9.S4, V9.S4 + VEOR V10.B16, V5.B16, V16.B16 + VEOR V11.B16, V6.B16, V17.B16 + VEOR V8.B16, V7.B16, V18.B16 + VEOR V9.B16, V4.B16, V19.B16 + VSHL $12, V16.S4, V5.S4 + VSHL $12, V17.S4, V6.S4 + VSHL $12, V18.S4, V7.S4 + VSHL $12, V19.S4, V4.S4 + VSRI $20, V16.S4, V5.S4 + VSRI $20, V17.S4, V6.S4 + VSRI $20, V18.S4, V7.S4 + VSRI $20, V19.S4, V4.S4 + + // V0 += V5; V15 <<<= ((V0 XOR V15), 8) + // ... + VADD V5.S4, V0.S4, V0.S4 + VADD V6.S4, V1.S4, V1.S4 + VADD V7.S4, V2.S4, V2.S4 + VADD V4.S4, V3.S4, V3.S4 + VEOR V0.B16, V15.B16, V15.B16 + VEOR V1.B16, V12.B16, V12.B16 + VEOR V2.B16, V13.B16, V13.B16 + VEOR V3.B16, V14.B16, V14.B16 + VTBL V31.B16, [V12.B16], V12.B16 + VTBL V31.B16, [V13.B16], V13.B16 + VTBL V31.B16, [V14.B16], V14.B16 + VTBL V31.B16, [V15.B16], V15.B16 + + // V10 += V15; V5 <<<= ((V10 XOR V5), 7) + // ... + VADD V15.S4, V10.S4, V10.S4 + VADD V12.S4, V11.S4, V11.S4 + VADD V13.S4, V8.S4, V8.S4 + VADD V14.S4, V9.S4, V9.S4 + VEOR V10.B16, V5.B16, V16.B16 + VEOR V11.B16, V6.B16, V17.B16 + VEOR V8.B16, V7.B16, V18.B16 + VEOR V9.B16, V4.B16, V19.B16 + VSHL $7, V16.S4, V5.S4 + VSHL $7, V17.S4, V6.S4 + VSHL $7, V18.S4, V7.S4 + VSHL $7, V19.S4, V4.S4 + VSRI $25, V16.S4, V5.S4 + VSRI $25, V17.S4, V6.S4 + VSRI $25, V18.S4, V7.S4 + VSRI $25, V19.S4, V4.S4 + + SUB $1, R21 + CBNZ R21, chacha + + // VLD4R (R10), [V16.S4, V17.S4, V18.S4, V19.S4] + WORD $0x4D60E950 + + // VLD4R 16(R4), [V20.S4, V21.S4, V22.S4, V23.S4] + WORD $0x4DFFE894 + VADD V30.S4, V12.S4, V12.S4 + VADD V16.S4, V0.S4, V0.S4 + VADD V17.S4, V1.S4, V1.S4 + VADD V18.S4, V2.S4, V2.S4 + VADD V19.S4, V3.S4, V3.S4 + // VLD4R 16(R4), [V24.S4, V25.S4, V26.S4, V27.S4] + WORD $0x4DFFE898 + // restore R4 + SUB $32, R4 + + // load counter + nonce + // VLD1R (R7), [V28.S4] + WORD $0x4D40C8FC + // VLD3R (R6), [V29.S4, V30.S4, V31.S4] + WORD $0x4D40E8DD + + VADD V20.S4, V4.S4, V4.S4 + VADD V21.S4, V5.S4, V5.S4 + VADD V22.S4, V6.S4, V6.S4 + VADD V23.S4, V7.S4, V7.S4 + VADD V24.S4, V8.S4, V8.S4 + VADD V25.S4, V9.S4, V9.S4 + VADD V26.S4, V10.S4, V10.S4 + VADD V27.S4, V11.S4, V11.S4 + VADD V28.S4, V12.S4, V12.S4 + VADD V29.S4, V13.S4, V13.S4 + VADD V30.S4, V14.S4, V14.S4 + VADD V31.S4, V15.S4, V15.S4 + + VZIP1 V1.S4, V0.S4, V16.S4 + VZIP2 V1.S4, V0.S4, V17.S4 + VZIP1 V3.S4, V2.S4, V18.S4 + VZIP2 V3.S4, V2.S4, V19.S4 + VZIP1 V5.S4, V4.S4, V20.S4 + VZIP2 V5.S4, V4.S4, V21.S4 + VZIP1 V7.S4, V6.S4, V22.S4 + VZIP2 V7.S4, V6.S4, V23.S4 + VZIP1 V9.S4, V8.S4, V24.S4 + VZIP2 V9.S4, V8.S4, V25.S4 + VZIP1 V11.S4, V10.S4, V26.S4 + VZIP2 V11.S4, V10.S4, V27.S4 + VZIP1 V13.S4, V12.S4, V28.S4 + VZIP2 V13.S4, V12.S4, V29.S4 + VZIP1 V15.S4, V14.S4, V30.S4 + VZIP2 V15.S4, V14.S4, V31.S4 + VZIP1 V18.D2, V16.D2, V0.D2 + VZIP2 V18.D2, V16.D2, V4.D2 + VZIP1 V19.D2, V17.D2, V8.D2 + VZIP2 V19.D2, V17.D2, V12.D2 + VLD1.P 64(R2), [V16.B16, V17.B16, V18.B16, V19.B16] + + VZIP1 V22.D2, V20.D2, V1.D2 + VZIP2 V22.D2, V20.D2, V5.D2 + VZIP1 V23.D2, V21.D2, V9.D2 + VZIP2 V23.D2, V21.D2, V13.D2 + VLD1.P 64(R2), [V20.B16, V21.B16, V22.B16, V23.B16] + VZIP1 V26.D2, V24.D2, V2.D2 + VZIP2 V26.D2, V24.D2, V6.D2 + VZIP1 V27.D2, V25.D2, V10.D2 + VZIP2 V27.D2, V25.D2, V14.D2 + VLD1.P 64(R2), [V24.B16, V25.B16, V26.B16, V27.B16] + VZIP1 V30.D2, V28.D2, V3.D2 + VZIP2 V30.D2, V28.D2, V7.D2 + VZIP1 V31.D2, V29.D2, V11.D2 + VZIP2 V31.D2, V29.D2, V15.D2 + VLD1.P 64(R2), [V28.B16, V29.B16, V30.B16, V31.B16] + VEOR V0.B16, V16.B16, V16.B16 + VEOR V1.B16, V17.B16, V17.B16 + VEOR V2.B16, V18.B16, V18.B16 + VEOR V3.B16, V19.B16, V19.B16 + VST1.P [V16.B16, V17.B16, V18.B16, V19.B16], 64(R1) + VEOR V4.B16, V20.B16, V20.B16 + VEOR V5.B16, V21.B16, V21.B16 + VEOR V6.B16, V22.B16, V22.B16 + VEOR V7.B16, V23.B16, V23.B16 + VST1.P [V20.B16, V21.B16, V22.B16, V23.B16], 64(R1) + VEOR V8.B16, V24.B16, V24.B16 + VEOR V9.B16, V25.B16, V25.B16 + VEOR V10.B16, V26.B16, V26.B16 + VEOR V11.B16, V27.B16, V27.B16 + VST1.P [V24.B16, V25.B16, V26.B16, V27.B16], 64(R1) + VEOR V12.B16, V28.B16, V28.B16 + VEOR V13.B16, V29.B16, V29.B16 + VEOR V14.B16, V30.B16, V30.B16 + VEOR V15.B16, V31.B16, V31.B16 + VST1.P [V28.B16, V29.B16, V30.B16, V31.B16], 64(R1) + + ADD $4, R20 + MOVW R20, (R7) // update counter + + CMP R2, R12 + BGT loop + + RET + + +DATA ·constants+0x00(SB)/4, $0x61707865 +DATA ·constants+0x04(SB)/4, $0x3320646e +DATA ·constants+0x08(SB)/4, $0x79622d32 +DATA ·constants+0x0c(SB)/4, $0x6b206574 +GLOBL ·constants(SB), NOPTR|RODATA, $32 + +DATA ·incRotMatrix+0x00(SB)/4, $0x00000000 +DATA ·incRotMatrix+0x04(SB)/4, $0x00000001 +DATA ·incRotMatrix+0x08(SB)/4, $0x00000002 +DATA ·incRotMatrix+0x0c(SB)/4, $0x00000003 +DATA ·incRotMatrix+0x10(SB)/4, $0x02010003 +DATA ·incRotMatrix+0x14(SB)/4, $0x06050407 +DATA ·incRotMatrix+0x18(SB)/4, $0x0A09080B +DATA ·incRotMatrix+0x1c(SB)/4, $0x0E0D0C0F +GLOBL ·incRotMatrix(SB), NOPTR|RODATA, $32 diff --git a/vendor/golang.org/x/crypto/internal/chacha20/chacha_arm64.go b/vendor/golang.org/x/crypto/internal/chacha20/chacha_arm64.go new file mode 100644 index 00000000000..ad74e23aef4 --- /dev/null +++ b/vendor/golang.org/x/crypto/internal/chacha20/chacha_arm64.go @@ -0,0 +1,31 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build go1.11 +// +build !gccgo + +package chacha20 + +const ( + haveAsm = true + bufSize = 256 +) + +//go:noescape +func xorKeyStreamVX(dst, src []byte, key *[8]uint32, nonce *[3]uint32, counter *uint32) + +func (c *Cipher) xorKeyStreamAsm(dst, src []byte) { + + if len(src) >= bufSize { + xorKeyStreamVX(dst, src, &c.key, &c.nonce, &c.counter) + } + + if len(src)%bufSize != 0 { + i := len(src) - len(src)%bufSize + c.buf = [bufSize]byte{} + copy(c.buf[:], src[i:]) + xorKeyStreamVX(c.buf[:], c.buf[:], &c.key, &c.nonce, &c.counter) + c.len = bufSize - copy(dst[i:], c.buf[:len(src)%bufSize]) + } +} diff --git a/vendor/golang.org/x/crypto/internal/chacha20/chacha_noasm.go b/vendor/golang.org/x/crypto/internal/chacha20/chacha_noasm.go index 91520d1de07..47eac0314c9 100644 --- a/vendor/golang.org/x/crypto/internal/chacha20/chacha_noasm.go +++ b/vendor/golang.org/x/crypto/internal/chacha20/chacha_noasm.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build !s390x gccgo appengine +// +build !arm64,!s390x arm64,!go1.11 gccgo appengine package chacha20 diff --git a/vendor/golang.org/x/crypto/pkcs12/pkcs12.go b/vendor/golang.org/x/crypto/pkcs12/pkcs12.go index eff9ad3a98f..55f7691d48d 100644 --- a/vendor/golang.org/x/crypto/pkcs12/pkcs12.go +++ b/vendor/golang.org/x/crypto/pkcs12/pkcs12.go @@ -7,6 +7,9 @@ // This implementation is distilled from https://tools.ietf.org/html/rfc7292 // and referenced documents. It is intended for decoding P12/PFX-stored // certificates and keys for use with the crypto/tls package. +// +// This package is frozen. If it's missing functionality you need, consider +// an alternative like software.sslmate.com/src/go-pkcs12. package pkcs12 import ( @@ -100,7 +103,7 @@ func unmarshal(in []byte, out interface{}) error { return nil } -// ConvertToPEM converts all "safe bags" contained in pfxData to PEM blocks. +// ToPEM converts all "safe bags" contained in pfxData to PEM blocks. func ToPEM(pfxData []byte, password string) ([]*pem.Block, error) { encodedPassword, err := bmpString(password) if err != nil { @@ -208,7 +211,7 @@ func convertAttribute(attribute *pkcs12Attribute) (key, value string, err error) // Decode extracts a certificate and private key from pfxData. This function // assumes that there is only one certificate and only one private key in the -// pfxData. +// pfxData; if there are more use ToPEM instead. func Decode(pfxData []byte, password string) (privateKey interface{}, certificate *x509.Certificate, err error) { encodedPassword, err := bmpString(password) if err != nil { diff --git a/vendor/golang.org/x/crypto/ssh/terminal/terminal.go b/vendor/golang.org/x/crypto/ssh/terminal/terminal.go index 9a887598ff4..9d666ffcf0f 100644 --- a/vendor/golang.org/x/crypto/ssh/terminal/terminal.go +++ b/vendor/golang.org/x/crypto/ssh/terminal/terminal.go @@ -159,6 +159,10 @@ func bytesToKey(b []byte, pasteActive bool) (rune, []byte) { return keyClearScreen, b[1:] case 23: // ^W return keyDeleteWord, b[1:] + case 14: // ^N + return keyDown, b[1:] + case 16: // ^P + return keyUp, b[1:] } } diff --git a/vendor/golang.org/x/crypto/ssh/terminal/util.go b/vendor/golang.org/x/crypto/ssh/terminal/util.go index 731c89a284a..39110408409 100644 --- a/vendor/golang.org/x/crypto/ssh/terminal/util.go +++ b/vendor/golang.org/x/crypto/ssh/terminal/util.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin dragonfly freebsd linux,!appengine netbsd openbsd +// +build aix darwin dragonfly freebsd linux,!appengine netbsd openbsd // Package terminal provides support functions for dealing with terminals, as // commonly found on UNIX systems. @@ -25,7 +25,7 @@ type State struct { termios unix.Termios } -// IsTerminal returns true if the given file descriptor is a terminal. +// IsTerminal returns whether the given file descriptor is a terminal. func IsTerminal(fd int) bool { _, err := unix.IoctlGetTermios(fd, ioctlReadTermios) return err == nil diff --git a/vendor/golang.org/x/crypto/ssh/terminal/util_aix.go b/vendor/golang.org/x/crypto/ssh/terminal/util_aix.go new file mode 100644 index 00000000000..dfcd6278592 --- /dev/null +++ b/vendor/golang.org/x/crypto/ssh/terminal/util_aix.go @@ -0,0 +1,12 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build aix + +package terminal + +import "golang.org/x/sys/unix" + +const ioctlReadTermios = unix.TCGETS +const ioctlWriteTermios = unix.TCSETS diff --git a/vendor/golang.org/x/crypto/ssh/terminal/util_plan9.go b/vendor/golang.org/x/crypto/ssh/terminal/util_plan9.go index 799f049f04e..9317ac7ede6 100644 --- a/vendor/golang.org/x/crypto/ssh/terminal/util_plan9.go +++ b/vendor/golang.org/x/crypto/ssh/terminal/util_plan9.go @@ -21,7 +21,7 @@ import ( type State struct{} -// IsTerminal returns true if the given file descriptor is a terminal. +// IsTerminal returns whether the given file descriptor is a terminal. func IsTerminal(fd int) bool { return false } diff --git a/vendor/golang.org/x/crypto/ssh/terminal/util_solaris.go b/vendor/golang.org/x/crypto/ssh/terminal/util_solaris.go index 9e41b9f43f0..3d5f06a9f04 100644 --- a/vendor/golang.org/x/crypto/ssh/terminal/util_solaris.go +++ b/vendor/golang.org/x/crypto/ssh/terminal/util_solaris.go @@ -17,7 +17,7 @@ type State struct { termios unix.Termios } -// IsTerminal returns true if the given file descriptor is a terminal. +// IsTerminal returns whether the given file descriptor is a terminal. func IsTerminal(fd int) bool { _, err := unix.IoctlGetTermio(fd, unix.TCGETA) return err == nil diff --git a/vendor/golang.org/x/crypto/ssh/terminal/util_windows.go b/vendor/golang.org/x/crypto/ssh/terminal/util_windows.go index 8618955df73..6cb8a95038c 100644 --- a/vendor/golang.org/x/crypto/ssh/terminal/util_windows.go +++ b/vendor/golang.org/x/crypto/ssh/terminal/util_windows.go @@ -26,7 +26,7 @@ type State struct { mode uint32 } -// IsTerminal returns true if the given file descriptor is a terminal. +// IsTerminal returns whether the given file descriptor is a terminal. func IsTerminal(fd int) bool { var st uint32 err := windows.GetConsoleMode(windows.Handle(fd), &st) diff --git a/vendor/golang.org/x/net/html/node.go b/vendor/golang.org/x/net/html/node.go index 2c1cade6070..633ee15dc55 100644 --- a/vendor/golang.org/x/net/html/node.go +++ b/vendor/golang.org/x/net/html/node.go @@ -177,7 +177,7 @@ func (s *nodeStack) index(n *Node) int { // contains returns whether a is within s. func (s *nodeStack) contains(a atom.Atom) bool { for _, n := range *s { - if n.DataAtom == a { + if n.DataAtom == a && n.Namespace == "" { return true } } diff --git a/vendor/golang.org/x/net/html/parse.go b/vendor/golang.org/x/net/html/parse.go index 64a5793725b..ca2cb5875b2 100644 --- a/vendor/golang.org/x/net/html/parse.go +++ b/vendor/golang.org/x/net/html/parse.go @@ -439,9 +439,6 @@ func (p *parser) resetInsertionMode() { case a.Select: if !last { for ancestor, first := n, p.oe[0]; ancestor != first; { - if ancestor == first { - break - } ancestor = p.oe[p.oe.index(ancestor)-1] switch ancestor.DataAtom { case a.Template: @@ -1719,8 +1716,12 @@ func inSelectIM(p *parser) bool { } p.addElement() case a.Select: - p.tok.Type = EndTagToken - return false + if p.popUntil(selectScope, a.Select) { + p.resetInsertionMode() + } else { + // Ignore the token. + return true + } case a.Input, a.Keygen, a.Textarea: if p.elementInScope(selectScope, a.Select) { p.parseImpliedToken(EndTagToken, a.Select, a.Select.String()) @@ -1750,6 +1751,9 @@ func inSelectIM(p *parser) bool { case a.Select: if p.popUntil(selectScope, a.Select) { p.resetInsertionMode() + } else { + // Ignore the token. + return true } case a.Template: return inHeadIM(p) @@ -1775,13 +1779,22 @@ func inSelectInTableIM(p *parser) bool { case StartTagToken, EndTagToken: switch p.tok.DataAtom { case a.Caption, a.Table, a.Tbody, a.Tfoot, a.Thead, a.Tr, a.Td, a.Th: - if p.tok.Type == StartTagToken || p.elementInScope(tableScope, p.tok.DataAtom) { - p.parseImpliedToken(EndTagToken, a.Select, a.Select.String()) - return false - } else { + if p.tok.Type == EndTagToken && !p.elementInScope(tableScope, p.tok.DataAtom) { // Ignore the token. return true } + // This is like p.popUntil(selectScope, a.Select), but it also + // matches , not just