repo
stringlengths 5
54
| path
stringlengths 4
155
| func_name
stringlengths 1
118
| original_string
stringlengths 52
85.5k
| language
stringclasses 1
value | code
stringlengths 52
85.5k
| code_tokens
sequence | docstring
stringlengths 6
2.61k
| docstring_tokens
sequence | sha
stringlengths 40
40
| url
stringlengths 85
252
| partition
stringclasses 1
value |
---|---|---|---|---|---|---|---|---|---|---|---|
gravitational/teleport | lib/sshutils/signer.go | NewSigner | func NewSigner(keyBytes, certBytes []byte) (ssh.Signer, error) {
keySigner, err := ssh.ParsePrivateKey(keyBytes)
if err != nil {
return nil, trace.Wrap(err, "failed to parse SSH private key")
}
pubkey, _, _, _, err := ssh.ParseAuthorizedKey(certBytes)
if err != nil {
return nil, trace.Wrap(err, "failed to parse SSH certificate")
}
cert, ok := pubkey.(*ssh.Certificate)
if !ok {
return nil, trace.BadParameter("expected SSH certificate, got %T ", pubkey)
}
return ssh.NewCertSigner(cert, keySigner)
} | go | func NewSigner(keyBytes, certBytes []byte) (ssh.Signer, error) {
keySigner, err := ssh.ParsePrivateKey(keyBytes)
if err != nil {
return nil, trace.Wrap(err, "failed to parse SSH private key")
}
pubkey, _, _, _, err := ssh.ParseAuthorizedKey(certBytes)
if err != nil {
return nil, trace.Wrap(err, "failed to parse SSH certificate")
}
cert, ok := pubkey.(*ssh.Certificate)
if !ok {
return nil, trace.BadParameter("expected SSH certificate, got %T ", pubkey)
}
return ssh.NewCertSigner(cert, keySigner)
} | [
"func",
"NewSigner",
"(",
"keyBytes",
",",
"certBytes",
"[",
"]",
"byte",
")",
"(",
"ssh",
".",
"Signer",
",",
"error",
")",
"{",
"keySigner",
",",
"err",
":=",
"ssh",
".",
"ParsePrivateKey",
"(",
"keyBytes",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"pubkey",
",",
"_",
",",
"_",
",",
"_",
",",
"err",
":=",
"ssh",
".",
"ParseAuthorizedKey",
"(",
"certBytes",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"cert",
",",
"ok",
":=",
"pubkey",
".",
"(",
"*",
"ssh",
".",
"Certificate",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"pubkey",
")",
"\n",
"}",
"\n\n",
"return",
"ssh",
".",
"NewCertSigner",
"(",
"cert",
",",
"keySigner",
")",
"\n",
"}"
] | // NewSigner returns new ssh Signer from private key + certificate pair. The
// signer can be used to create "auth methods" i.e. login into Teleport SSH
// servers. | [
"NewSigner",
"returns",
"new",
"ssh",
"Signer",
"from",
"private",
"key",
"+",
"certificate",
"pair",
".",
"The",
"signer",
"can",
"be",
"used",
"to",
"create",
"auth",
"methods",
"i",
".",
"e",
".",
"login",
"into",
"Teleport",
"SSH",
"servers",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/sshutils/signer.go#L29-L46 | train |
gravitational/teleport | lib/sshutils/signer.go | CryptoPublicKey | func CryptoPublicKey(publicKey []byte) (crypto.PublicKey, error) {
// reuse the same RSA keys for SSH and TLS keys
pubKey, _, _, _, err := ssh.ParseAuthorizedKey(publicKey)
if err != nil {
return nil, trace.Wrap(err)
}
cryptoPubKey, ok := pubKey.(ssh.CryptoPublicKey)
if !ok {
return nil, trace.BadParameter("expected ssh.CryptoPublicKey, got %T", pubKey)
}
return cryptoPubKey.CryptoPublicKey(), nil
} | go | func CryptoPublicKey(publicKey []byte) (crypto.PublicKey, error) {
// reuse the same RSA keys for SSH and TLS keys
pubKey, _, _, _, err := ssh.ParseAuthorizedKey(publicKey)
if err != nil {
return nil, trace.Wrap(err)
}
cryptoPubKey, ok := pubKey.(ssh.CryptoPublicKey)
if !ok {
return nil, trace.BadParameter("expected ssh.CryptoPublicKey, got %T", pubKey)
}
return cryptoPubKey.CryptoPublicKey(), nil
} | [
"func",
"CryptoPublicKey",
"(",
"publicKey",
"[",
"]",
"byte",
")",
"(",
"crypto",
".",
"PublicKey",
",",
"error",
")",
"{",
"// reuse the same RSA keys for SSH and TLS keys",
"pubKey",
",",
"_",
",",
"_",
",",
"_",
",",
"err",
":=",
"ssh",
".",
"ParseAuthorizedKey",
"(",
"publicKey",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"cryptoPubKey",
",",
"ok",
":=",
"pubKey",
".",
"(",
"ssh",
".",
"CryptoPublicKey",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"pubKey",
")",
"\n",
"}",
"\n",
"return",
"cryptoPubKey",
".",
"CryptoPublicKey",
"(",
")",
",",
"nil",
"\n",
"}"
] | // CryptoPublicKey extracts public key from RSA public key in authorized_keys format | [
"CryptoPublicKey",
"extracts",
"public",
"key",
"from",
"RSA",
"public",
"key",
"in",
"authorized_keys",
"format"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/sshutils/signer.go#L49-L60 | train |
gravitational/teleport | lib/tlsca/parsegen.go | ClusterName | func ClusterName(subject pkix.Name) (string, error) {
if len(subject.Organization) == 0 {
return "", trace.BadParameter("missing subject organization")
}
return subject.Organization[0], nil
} | go | func ClusterName(subject pkix.Name) (string, error) {
if len(subject.Organization) == 0 {
return "", trace.BadParameter("missing subject organization")
}
return subject.Organization[0], nil
} | [
"func",
"ClusterName",
"(",
"subject",
"pkix",
".",
"Name",
")",
"(",
"string",
",",
"error",
")",
"{",
"if",
"len",
"(",
"subject",
".",
"Organization",
")",
"==",
"0",
"{",
"return",
"\"",
"\"",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"subject",
".",
"Organization",
"[",
"0",
"]",
",",
"nil",
"\n",
"}"
] | // ClusterName returns cluster name from organization | [
"ClusterName",
"returns",
"cluster",
"name",
"from",
"organization"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/tlsca/parsegen.go#L36-L41 | train |
gravitational/teleport | lib/tlsca/parsegen.go | GenerateRSAPrivateKeyPEM | func GenerateRSAPrivateKeyPEM() ([]byte, error) {
priv, err := rsa.GenerateKey(rand.Reader, teleport.RSAKeySize)
if err != nil {
return nil, trace.Wrap(err)
}
return pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}), nil
} | go | func GenerateRSAPrivateKeyPEM() ([]byte, error) {
priv, err := rsa.GenerateKey(rand.Reader, teleport.RSAKeySize)
if err != nil {
return nil, trace.Wrap(err)
}
return pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}), nil
} | [
"func",
"GenerateRSAPrivateKeyPEM",
"(",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"priv",
",",
"err",
":=",
"rsa",
".",
"GenerateKey",
"(",
"rand",
".",
"Reader",
",",
"teleport",
".",
"RSAKeySize",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"return",
"pem",
".",
"EncodeToMemory",
"(",
"&",
"pem",
".",
"Block",
"{",
"Type",
":",
"\"",
"\"",
",",
"Bytes",
":",
"x509",
".",
"MarshalPKCS1PrivateKey",
"(",
"priv",
")",
"}",
")",
",",
"nil",
"\n",
"}"
] | // GenerateRSAPrivateKeyPEM generates new RSA private key and returns PEM encoded bytes | [
"GenerateRSAPrivateKeyPEM",
"generates",
"new",
"RSA",
"private",
"key",
"and",
"returns",
"PEM",
"encoded",
"bytes"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/tlsca/parsegen.go#L44-L50 | train |
gravitational/teleport | lib/tlsca/parsegen.go | ParsePublicKeyPEM | func ParsePublicKeyPEM(bytes []byte) (interface{}, error) {
block, _ := pem.Decode(bytes)
if block == nil {
return nil, trace.BadParameter("expected PEM-encoded block")
}
return ParsePublicKeyDER(block.Bytes)
} | go | func ParsePublicKeyPEM(bytes []byte) (interface{}, error) {
block, _ := pem.Decode(bytes)
if block == nil {
return nil, trace.BadParameter("expected PEM-encoded block")
}
return ParsePublicKeyDER(block.Bytes)
} | [
"func",
"ParsePublicKeyPEM",
"(",
"bytes",
"[",
"]",
"byte",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"block",
",",
"_",
":=",
"pem",
".",
"Decode",
"(",
"bytes",
")",
"\n",
"if",
"block",
"==",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"ParsePublicKeyDER",
"(",
"block",
".",
"Bytes",
")",
"\n",
"}"
] | // ParsePublicKeyPEM parses public key PEM | [
"ParsePublicKeyPEM",
"parses",
"public",
"key",
"PEM"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/tlsca/parsegen.go#L160-L166 | train |
gravitational/teleport | lib/tlsca/parsegen.go | ParsePublicKeyDER | func ParsePublicKeyDER(der []byte) (crypto.PublicKey, error) {
generalKey, err := x509.ParsePKIXPublicKey(der)
if err != nil {
return nil, trace.Wrap(err)
}
return generalKey, nil
} | go | func ParsePublicKeyDER(der []byte) (crypto.PublicKey, error) {
generalKey, err := x509.ParsePKIXPublicKey(der)
if err != nil {
return nil, trace.Wrap(err)
}
return generalKey, nil
} | [
"func",
"ParsePublicKeyDER",
"(",
"der",
"[",
"]",
"byte",
")",
"(",
"crypto",
".",
"PublicKey",
",",
"error",
")",
"{",
"generalKey",
",",
"err",
":=",
"x509",
".",
"ParsePKIXPublicKey",
"(",
"der",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"return",
"generalKey",
",",
"nil",
"\n",
"}"
] | // ParsePublicKeyDER parses unencrypted DER-encoded publice key | [
"ParsePublicKeyDER",
"parses",
"unencrypted",
"DER",
"-",
"encoded",
"publice",
"key"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/tlsca/parsegen.go#L169-L175 | train |
gravitational/teleport | lib/tlsca/parsegen.go | MarshalPublicKeyFromPrivateKeyPEM | func MarshalPublicKeyFromPrivateKeyPEM(privateKey crypto.PrivateKey) ([]byte, error) {
rsaPrivateKey, ok := privateKey.(*rsa.PrivateKey)
if !ok {
return nil, trace.BadParameter("expected RSA key")
}
rsaPublicKey := rsaPrivateKey.Public()
derBytes, err := x509.MarshalPKIXPublicKey(rsaPublicKey)
if err != nil {
return nil, trace.Wrap(err)
}
return pem.EncodeToMemory(&pem.Block{Type: "RSA PUBLIC KEY", Bytes: derBytes}), nil
} | go | func MarshalPublicKeyFromPrivateKeyPEM(privateKey crypto.PrivateKey) ([]byte, error) {
rsaPrivateKey, ok := privateKey.(*rsa.PrivateKey)
if !ok {
return nil, trace.BadParameter("expected RSA key")
}
rsaPublicKey := rsaPrivateKey.Public()
derBytes, err := x509.MarshalPKIXPublicKey(rsaPublicKey)
if err != nil {
return nil, trace.Wrap(err)
}
return pem.EncodeToMemory(&pem.Block{Type: "RSA PUBLIC KEY", Bytes: derBytes}), nil
} | [
"func",
"MarshalPublicKeyFromPrivateKeyPEM",
"(",
"privateKey",
"crypto",
".",
"PrivateKey",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"rsaPrivateKey",
",",
"ok",
":=",
"privateKey",
".",
"(",
"*",
"rsa",
".",
"PrivateKey",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"rsaPublicKey",
":=",
"rsaPrivateKey",
".",
"Public",
"(",
")",
"\n",
"derBytes",
",",
"err",
":=",
"x509",
".",
"MarshalPKIXPublicKey",
"(",
"rsaPublicKey",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"return",
"pem",
".",
"EncodeToMemory",
"(",
"&",
"pem",
".",
"Block",
"{",
"Type",
":",
"\"",
"\"",
",",
"Bytes",
":",
"derBytes",
"}",
")",
",",
"nil",
"\n",
"}"
] | // MarshalPublicKeyFromPrivateKeyPEM extracts public key from private key
// and returns PEM marshalled key | [
"MarshalPublicKeyFromPrivateKeyPEM",
"extracts",
"public",
"key",
"from",
"private",
"key",
"and",
"returns",
"PEM",
"marshalled",
"key"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/tlsca/parsegen.go#L179-L190 | train |
gravitational/teleport | lib/auth/init.go | isFirstStart | func isFirstStart(authServer *AuthServer, cfg InitConfig) (bool, error) {
// check if the CA exists?
_, err := authServer.GetCertAuthority(
services.CertAuthID{
DomainName: cfg.ClusterName.GetClusterName(),
Type: services.HostCA,
}, false)
if err != nil {
if !trace.IsNotFound(err) {
return false, trace.Wrap(err)
}
// CA not found? --> first start!
return true, nil
}
return false, nil
} | go | func isFirstStart(authServer *AuthServer, cfg InitConfig) (bool, error) {
// check if the CA exists?
_, err := authServer.GetCertAuthority(
services.CertAuthID{
DomainName: cfg.ClusterName.GetClusterName(),
Type: services.HostCA,
}, false)
if err != nil {
if !trace.IsNotFound(err) {
return false, trace.Wrap(err)
}
// CA not found? --> first start!
return true, nil
}
return false, nil
} | [
"func",
"isFirstStart",
"(",
"authServer",
"*",
"AuthServer",
",",
"cfg",
"InitConfig",
")",
"(",
"bool",
",",
"error",
")",
"{",
"// check if the CA exists?",
"_",
",",
"err",
":=",
"authServer",
".",
"GetCertAuthority",
"(",
"services",
".",
"CertAuthID",
"{",
"DomainName",
":",
"cfg",
".",
"ClusterName",
".",
"GetClusterName",
"(",
")",
",",
"Type",
":",
"services",
".",
"HostCA",
",",
"}",
",",
"false",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"if",
"!",
"trace",
".",
"IsNotFound",
"(",
"err",
")",
"{",
"return",
"false",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"// CA not found? --> first start!",
"return",
"true",
",",
"nil",
"\n",
"}",
"\n",
"return",
"false",
",",
"nil",
"\n",
"}"
] | // isFirstStart returns 'true' if the auth server is starting for the 1st time
// on this server. | [
"isFirstStart",
"returns",
"true",
"if",
"the",
"auth",
"server",
"is",
"starting",
"for",
"the",
"1st",
"time",
"on",
"this",
"server",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/auth/init.go#L476-L491 | train |
gravitational/teleport | lib/auth/init.go | GenerateIdentity | func GenerateIdentity(a *AuthServer, id IdentityID, additionalPrincipals, dnsNames []string) (*Identity, error) {
keys, err := a.GenerateServerKeys(GenerateServerKeysRequest{
HostID: id.HostUUID,
NodeName: id.NodeName,
Roles: teleport.Roles{id.Role},
AdditionalPrincipals: additionalPrincipals,
DNSNames: dnsNames,
})
if err != nil {
return nil, trace.Wrap(err)
}
return ReadIdentityFromKeyPair(keys)
} | go | func GenerateIdentity(a *AuthServer, id IdentityID, additionalPrincipals, dnsNames []string) (*Identity, error) {
keys, err := a.GenerateServerKeys(GenerateServerKeysRequest{
HostID: id.HostUUID,
NodeName: id.NodeName,
Roles: teleport.Roles{id.Role},
AdditionalPrincipals: additionalPrincipals,
DNSNames: dnsNames,
})
if err != nil {
return nil, trace.Wrap(err)
}
return ReadIdentityFromKeyPair(keys)
} | [
"func",
"GenerateIdentity",
"(",
"a",
"*",
"AuthServer",
",",
"id",
"IdentityID",
",",
"additionalPrincipals",
",",
"dnsNames",
"[",
"]",
"string",
")",
"(",
"*",
"Identity",
",",
"error",
")",
"{",
"keys",
",",
"err",
":=",
"a",
".",
"GenerateServerKeys",
"(",
"GenerateServerKeysRequest",
"{",
"HostID",
":",
"id",
".",
"HostUUID",
",",
"NodeName",
":",
"id",
".",
"NodeName",
",",
"Roles",
":",
"teleport",
".",
"Roles",
"{",
"id",
".",
"Role",
"}",
",",
"AdditionalPrincipals",
":",
"additionalPrincipals",
",",
"DNSNames",
":",
"dnsNames",
",",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"return",
"ReadIdentityFromKeyPair",
"(",
"keys",
")",
"\n",
"}"
] | // GenerateIdentity generates identity for the auth server | [
"GenerateIdentity",
"generates",
"identity",
"for",
"the",
"auth",
"server"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/auth/init.go#L494-L506 | train |
gravitational/teleport | lib/auth/init.go | String | func (i *Identity) String() string {
var out []string
if i.XCert != nil {
out = append(out, fmt.Sprintf("cert(%v issued by %v:%v)", i.XCert.Subject.CommonName, i.XCert.Issuer.CommonName, i.XCert.Issuer.SerialNumber))
}
for j := range i.TLSCACertsBytes {
cert, err := tlsca.ParseCertificatePEM(i.TLSCACertsBytes[j])
if err != nil {
out = append(out, err.Error())
} else {
out = append(out, fmt.Sprintf("trust root(%v:%v)", cert.Subject.CommonName, cert.Subject.SerialNumber))
}
}
return fmt.Sprintf("Identity(%v, %v)", i.ID.Role, strings.Join(out, ","))
} | go | func (i *Identity) String() string {
var out []string
if i.XCert != nil {
out = append(out, fmt.Sprintf("cert(%v issued by %v:%v)", i.XCert.Subject.CommonName, i.XCert.Issuer.CommonName, i.XCert.Issuer.SerialNumber))
}
for j := range i.TLSCACertsBytes {
cert, err := tlsca.ParseCertificatePEM(i.TLSCACertsBytes[j])
if err != nil {
out = append(out, err.Error())
} else {
out = append(out, fmt.Sprintf("trust root(%v:%v)", cert.Subject.CommonName, cert.Subject.SerialNumber))
}
}
return fmt.Sprintf("Identity(%v, %v)", i.ID.Role, strings.Join(out, ","))
} | [
"func",
"(",
"i",
"*",
"Identity",
")",
"String",
"(",
")",
"string",
"{",
"var",
"out",
"[",
"]",
"string",
"\n",
"if",
"i",
".",
"XCert",
"!=",
"nil",
"{",
"out",
"=",
"append",
"(",
"out",
",",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"i",
".",
"XCert",
".",
"Subject",
".",
"CommonName",
",",
"i",
".",
"XCert",
".",
"Issuer",
".",
"CommonName",
",",
"i",
".",
"XCert",
".",
"Issuer",
".",
"SerialNumber",
")",
")",
"\n",
"}",
"\n",
"for",
"j",
":=",
"range",
"i",
".",
"TLSCACertsBytes",
"{",
"cert",
",",
"err",
":=",
"tlsca",
".",
"ParseCertificatePEM",
"(",
"i",
".",
"TLSCACertsBytes",
"[",
"j",
"]",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"out",
"=",
"append",
"(",
"out",
",",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"else",
"{",
"out",
"=",
"append",
"(",
"out",
",",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"cert",
".",
"Subject",
".",
"CommonName",
",",
"cert",
".",
"Subject",
".",
"SerialNumber",
")",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"i",
".",
"ID",
".",
"Role",
",",
"strings",
".",
"Join",
"(",
"out",
",",
"\"",
"\"",
")",
")",
"\n",
"}"
] | // String returns user-friendly representation of the identity. | [
"String",
"returns",
"user",
"-",
"friendly",
"representation",
"of",
"the",
"identity",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/auth/init.go#L534-L548 | train |
gravitational/teleport | lib/auth/init.go | CertInfo | func CertInfo(cert *x509.Certificate) string {
return fmt.Sprintf("cert(%v issued by %v:%v)", cert.Subject.CommonName, cert.Issuer.CommonName, cert.Issuer.SerialNumber)
} | go | func CertInfo(cert *x509.Certificate) string {
return fmt.Sprintf("cert(%v issued by %v:%v)", cert.Subject.CommonName, cert.Issuer.CommonName, cert.Issuer.SerialNumber)
} | [
"func",
"CertInfo",
"(",
"cert",
"*",
"x509",
".",
"Certificate",
")",
"string",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"cert",
".",
"Subject",
".",
"CommonName",
",",
"cert",
".",
"Issuer",
".",
"CommonName",
",",
"cert",
".",
"Issuer",
".",
"SerialNumber",
")",
"\n",
"}"
] | // CertInfo returns diagnostic information about certificate | [
"CertInfo",
"returns",
"diagnostic",
"information",
"about",
"certificate"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/auth/init.go#L551-L553 | train |
gravitational/teleport | lib/auth/init.go | TLSCertInfo | func TLSCertInfo(cert *tls.Certificate) string {
x509cert, err := x509.ParseCertificate(cert.Certificate[0])
if err != nil {
return err.Error()
}
return CertInfo(x509cert)
} | go | func TLSCertInfo(cert *tls.Certificate) string {
x509cert, err := x509.ParseCertificate(cert.Certificate[0])
if err != nil {
return err.Error()
}
return CertInfo(x509cert)
} | [
"func",
"TLSCertInfo",
"(",
"cert",
"*",
"tls",
".",
"Certificate",
")",
"string",
"{",
"x509cert",
",",
"err",
":=",
"x509",
".",
"ParseCertificate",
"(",
"cert",
".",
"Certificate",
"[",
"0",
"]",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
".",
"Error",
"(",
")",
"\n",
"}",
"\n",
"return",
"CertInfo",
"(",
"x509cert",
")",
"\n",
"}"
] | // TLSCertInfo returns diagnostic information about certificate | [
"TLSCertInfo",
"returns",
"diagnostic",
"information",
"about",
"certificate"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/auth/init.go#L556-L562 | train |
gravitational/teleport | lib/auth/init.go | CertAuthorityInfo | func CertAuthorityInfo(ca services.CertAuthority) string {
var out []string
for _, keyPair := range ca.GetTLSKeyPairs() {
cert, err := tlsca.ParseCertificatePEM(keyPair.Cert)
if err != nil {
out = append(out, err.Error())
} else {
out = append(out, fmt.Sprintf("trust root(%v:%v)", cert.Subject.CommonName, cert.Subject.SerialNumber))
}
}
return fmt.Sprintf("cert authority(state: %v, phase: %v, roots: %v)", ca.GetRotation().State, ca.GetRotation().Phase, strings.Join(out, ", "))
} | go | func CertAuthorityInfo(ca services.CertAuthority) string {
var out []string
for _, keyPair := range ca.GetTLSKeyPairs() {
cert, err := tlsca.ParseCertificatePEM(keyPair.Cert)
if err != nil {
out = append(out, err.Error())
} else {
out = append(out, fmt.Sprintf("trust root(%v:%v)", cert.Subject.CommonName, cert.Subject.SerialNumber))
}
}
return fmt.Sprintf("cert authority(state: %v, phase: %v, roots: %v)", ca.GetRotation().State, ca.GetRotation().Phase, strings.Join(out, ", "))
} | [
"func",
"CertAuthorityInfo",
"(",
"ca",
"services",
".",
"CertAuthority",
")",
"string",
"{",
"var",
"out",
"[",
"]",
"string",
"\n",
"for",
"_",
",",
"keyPair",
":=",
"range",
"ca",
".",
"GetTLSKeyPairs",
"(",
")",
"{",
"cert",
",",
"err",
":=",
"tlsca",
".",
"ParseCertificatePEM",
"(",
"keyPair",
".",
"Cert",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"out",
"=",
"append",
"(",
"out",
",",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"else",
"{",
"out",
"=",
"append",
"(",
"out",
",",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"cert",
".",
"Subject",
".",
"CommonName",
",",
"cert",
".",
"Subject",
".",
"SerialNumber",
")",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"ca",
".",
"GetRotation",
"(",
")",
".",
"State",
",",
"ca",
".",
"GetRotation",
"(",
")",
".",
"Phase",
",",
"strings",
".",
"Join",
"(",
"out",
",",
"\"",
"\"",
")",
")",
"\n",
"}"
] | // CertAuthorityInfo returns debugging information about certificate authority | [
"CertAuthorityInfo",
"returns",
"debugging",
"information",
"about",
"certificate",
"authority"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/auth/init.go#L565-L576 | train |
gravitational/teleport | lib/auth/init.go | HasTLSConfig | func (i *Identity) HasTLSConfig() bool {
return len(i.TLSCACertsBytes) != 0 && len(i.TLSCertBytes) != 0
} | go | func (i *Identity) HasTLSConfig() bool {
return len(i.TLSCACertsBytes) != 0 && len(i.TLSCertBytes) != 0
} | [
"func",
"(",
"i",
"*",
"Identity",
")",
"HasTLSConfig",
"(",
")",
"bool",
"{",
"return",
"len",
"(",
"i",
".",
"TLSCACertsBytes",
")",
"!=",
"0",
"&&",
"len",
"(",
"i",
".",
"TLSCertBytes",
")",
"!=",
"0",
"\n",
"}"
] | // HasTSLConfig returns true if this identity has TLS certificate and private key | [
"HasTSLConfig",
"returns",
"true",
"if",
"this",
"identity",
"has",
"TLS",
"certificate",
"and",
"private",
"key"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/auth/init.go#L579-L581 | train |
gravitational/teleport | lib/auth/init.go | HasPrincipals | func (i *Identity) HasPrincipals(additionalPrincipals []string) bool {
set := utils.StringsSet(i.Cert.ValidPrincipals)
for _, principal := range additionalPrincipals {
if _, ok := set[principal]; !ok {
return false
}
}
return true
} | go | func (i *Identity) HasPrincipals(additionalPrincipals []string) bool {
set := utils.StringsSet(i.Cert.ValidPrincipals)
for _, principal := range additionalPrincipals {
if _, ok := set[principal]; !ok {
return false
}
}
return true
} | [
"func",
"(",
"i",
"*",
"Identity",
")",
"HasPrincipals",
"(",
"additionalPrincipals",
"[",
"]",
"string",
")",
"bool",
"{",
"set",
":=",
"utils",
".",
"StringsSet",
"(",
"i",
".",
"Cert",
".",
"ValidPrincipals",
")",
"\n",
"for",
"_",
",",
"principal",
":=",
"range",
"additionalPrincipals",
"{",
"if",
"_",
",",
"ok",
":=",
"set",
"[",
"principal",
"]",
";",
"!",
"ok",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}"
] | // HasPrincipals returns whether identity has principals | [
"HasPrincipals",
"returns",
"whether",
"identity",
"has",
"principals"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/auth/init.go#L584-L592 | train |
gravitational/teleport | lib/auth/init.go | HasDNSNames | func (i *Identity) HasDNSNames(dnsNames []string) bool {
if i.XCert == nil {
return false
}
set := utils.StringsSet(i.XCert.DNSNames)
for _, dnsName := range dnsNames {
if _, ok := set[dnsName]; !ok {
return false
}
}
return true
} | go | func (i *Identity) HasDNSNames(dnsNames []string) bool {
if i.XCert == nil {
return false
}
set := utils.StringsSet(i.XCert.DNSNames)
for _, dnsName := range dnsNames {
if _, ok := set[dnsName]; !ok {
return false
}
}
return true
} | [
"func",
"(",
"i",
"*",
"Identity",
")",
"HasDNSNames",
"(",
"dnsNames",
"[",
"]",
"string",
")",
"bool",
"{",
"if",
"i",
".",
"XCert",
"==",
"nil",
"{",
"return",
"false",
"\n",
"}",
"\n",
"set",
":=",
"utils",
".",
"StringsSet",
"(",
"i",
".",
"XCert",
".",
"DNSNames",
")",
"\n",
"for",
"_",
",",
"dnsName",
":=",
"range",
"dnsNames",
"{",
"if",
"_",
",",
"ok",
":=",
"set",
"[",
"dnsName",
"]",
";",
"!",
"ok",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}"
] | // HasDNSNames returns true if TLS certificate has required DNS names | [
"HasDNSNames",
"returns",
"true",
"if",
"TLS",
"certificate",
"has",
"required",
"DNS",
"names"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/auth/init.go#L595-L606 | train |
gravitational/teleport | lib/auth/init.go | TLSConfig | func (i *Identity) TLSConfig(cipherSuites []uint16) (*tls.Config, error) {
tlsConfig := utils.TLSConfig(cipherSuites)
if !i.HasTLSConfig() {
return nil, trace.NotFound("no TLS credentials setup for this identity")
}
tlsCert, err := tls.X509KeyPair(i.TLSCertBytes, i.KeyBytes)
if err != nil {
return nil, trace.BadParameter("failed to parse private key: %v", err)
}
certPool := x509.NewCertPool()
for j := range i.TLSCACertsBytes {
parsedCert, err := tlsca.ParseCertificatePEM(i.TLSCACertsBytes[j])
if err != nil {
return nil, trace.Wrap(err, "failed to parse CA certificate")
}
certPool.AddCert(parsedCert)
}
tlsConfig.Certificates = []tls.Certificate{tlsCert}
tlsConfig.RootCAs = certPool
tlsConfig.ClientCAs = certPool
tlsConfig.ServerName = EncodeClusterName(i.ClusterName)
return tlsConfig, nil
} | go | func (i *Identity) TLSConfig(cipherSuites []uint16) (*tls.Config, error) {
tlsConfig := utils.TLSConfig(cipherSuites)
if !i.HasTLSConfig() {
return nil, trace.NotFound("no TLS credentials setup for this identity")
}
tlsCert, err := tls.X509KeyPair(i.TLSCertBytes, i.KeyBytes)
if err != nil {
return nil, trace.BadParameter("failed to parse private key: %v", err)
}
certPool := x509.NewCertPool()
for j := range i.TLSCACertsBytes {
parsedCert, err := tlsca.ParseCertificatePEM(i.TLSCACertsBytes[j])
if err != nil {
return nil, trace.Wrap(err, "failed to parse CA certificate")
}
certPool.AddCert(parsedCert)
}
tlsConfig.Certificates = []tls.Certificate{tlsCert}
tlsConfig.RootCAs = certPool
tlsConfig.ClientCAs = certPool
tlsConfig.ServerName = EncodeClusterName(i.ClusterName)
return tlsConfig, nil
} | [
"func",
"(",
"i",
"*",
"Identity",
")",
"TLSConfig",
"(",
"cipherSuites",
"[",
"]",
"uint16",
")",
"(",
"*",
"tls",
".",
"Config",
",",
"error",
")",
"{",
"tlsConfig",
":=",
"utils",
".",
"TLSConfig",
"(",
"cipherSuites",
")",
"\n",
"if",
"!",
"i",
".",
"HasTLSConfig",
"(",
")",
"{",
"return",
"nil",
",",
"trace",
".",
"NotFound",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"tlsCert",
",",
"err",
":=",
"tls",
".",
"X509KeyPair",
"(",
"i",
".",
"TLSCertBytes",
",",
"i",
".",
"KeyBytes",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n",
"certPool",
":=",
"x509",
".",
"NewCertPool",
"(",
")",
"\n",
"for",
"j",
":=",
"range",
"i",
".",
"TLSCACertsBytes",
"{",
"parsedCert",
",",
"err",
":=",
"tlsca",
".",
"ParseCertificatePEM",
"(",
"i",
".",
"TLSCACertsBytes",
"[",
"j",
"]",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"certPool",
".",
"AddCert",
"(",
"parsedCert",
")",
"\n",
"}",
"\n",
"tlsConfig",
".",
"Certificates",
"=",
"[",
"]",
"tls",
".",
"Certificate",
"{",
"tlsCert",
"}",
"\n",
"tlsConfig",
".",
"RootCAs",
"=",
"certPool",
"\n",
"tlsConfig",
".",
"ClientCAs",
"=",
"certPool",
"\n",
"tlsConfig",
".",
"ServerName",
"=",
"EncodeClusterName",
"(",
"i",
".",
"ClusterName",
")",
"\n",
"return",
"tlsConfig",
",",
"nil",
"\n",
"}"
] | // TLSConfig returns TLS config for mutual TLS authentication
// can return NotFound error if there are no TLS credentials setup for identity | [
"TLSConfig",
"returns",
"TLS",
"config",
"for",
"mutual",
"TLS",
"authentication",
"can",
"return",
"NotFound",
"error",
"if",
"there",
"are",
"no",
"TLS",
"credentials",
"setup",
"for",
"identity"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/auth/init.go#L610-L632 | train |
gravitational/teleport | lib/auth/init.go | SSHClientConfig | func (i *Identity) SSHClientConfig() *ssh.ClientConfig {
return &ssh.ClientConfig{
User: i.ID.HostUUID,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(i.KeySigner),
},
HostKeyCallback: i.hostKeyCallback,
Timeout: defaults.DefaultDialTimeout,
}
} | go | func (i *Identity) SSHClientConfig() *ssh.ClientConfig {
return &ssh.ClientConfig{
User: i.ID.HostUUID,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(i.KeySigner),
},
HostKeyCallback: i.hostKeyCallback,
Timeout: defaults.DefaultDialTimeout,
}
} | [
"func",
"(",
"i",
"*",
"Identity",
")",
"SSHClientConfig",
"(",
")",
"*",
"ssh",
".",
"ClientConfig",
"{",
"return",
"&",
"ssh",
".",
"ClientConfig",
"{",
"User",
":",
"i",
".",
"ID",
".",
"HostUUID",
",",
"Auth",
":",
"[",
"]",
"ssh",
".",
"AuthMethod",
"{",
"ssh",
".",
"PublicKeys",
"(",
"i",
".",
"KeySigner",
")",
",",
"}",
",",
"HostKeyCallback",
":",
"i",
".",
"hostKeyCallback",
",",
"Timeout",
":",
"defaults",
".",
"DefaultDialTimeout",
",",
"}",
"\n",
"}"
] | // SSHClientConfig returns a ssh.ClientConfig used by nodes to connect to
// the reverse tunnel server. | [
"SSHClientConfig",
"returns",
"a",
"ssh",
".",
"ClientConfig",
"used",
"by",
"nodes",
"to",
"connect",
"to",
"the",
"reverse",
"tunnel",
"server",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/auth/init.go#L636-L645 | train |
gravitational/teleport | lib/auth/init.go | hostKeyCallback | func (i *Identity) hostKeyCallback(hostname string, remote net.Addr, key ssh.PublicKey) error {
cert, ok := key.(*ssh.Certificate)
if !ok {
return trace.BadParameter("only host certificates supported")
}
// Loop over all CAs and see if any of them signed the certificate.
for _, k := range i.SSHCACertBytes {
pubkey, _, _, _, err := ssh.ParseAuthorizedKey(k)
if err != nil {
return trace.Wrap(err)
}
if sshutils.KeysEqual(cert.SignatureKey, pubkey) {
return nil
}
}
return trace.BadParameter("no matching keys found")
} | go | func (i *Identity) hostKeyCallback(hostname string, remote net.Addr, key ssh.PublicKey) error {
cert, ok := key.(*ssh.Certificate)
if !ok {
return trace.BadParameter("only host certificates supported")
}
// Loop over all CAs and see if any of them signed the certificate.
for _, k := range i.SSHCACertBytes {
pubkey, _, _, _, err := ssh.ParseAuthorizedKey(k)
if err != nil {
return trace.Wrap(err)
}
if sshutils.KeysEqual(cert.SignatureKey, pubkey) {
return nil
}
}
return trace.BadParameter("no matching keys found")
} | [
"func",
"(",
"i",
"*",
"Identity",
")",
"hostKeyCallback",
"(",
"hostname",
"string",
",",
"remote",
"net",
".",
"Addr",
",",
"key",
"ssh",
".",
"PublicKey",
")",
"error",
"{",
"cert",
",",
"ok",
":=",
"key",
".",
"(",
"*",
"ssh",
".",
"Certificate",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"// Loop over all CAs and see if any of them signed the certificate.",
"for",
"_",
",",
"k",
":=",
"range",
"i",
".",
"SSHCACertBytes",
"{",
"pubkey",
",",
"_",
",",
"_",
",",
"_",
",",
"err",
":=",
"ssh",
".",
"ParseAuthorizedKey",
"(",
"k",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"if",
"sshutils",
".",
"KeysEqual",
"(",
"cert",
".",
"SignatureKey",
",",
"pubkey",
")",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}"
] | // hostKeyCallback checks if the host certificate was signed by any of the
// known CAs. | [
"hostKeyCallback",
"checks",
"if",
"the",
"host",
"certificate",
"was",
"signed",
"by",
"any",
"of",
"the",
"known",
"CAs",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/auth/init.go#L649-L667 | train |
gravitational/teleport | lib/auth/init.go | HostID | func (id *IdentityID) HostID() (string, error) {
parts := strings.Split(id.HostUUID, ".")
if len(parts) < 2 {
return "", trace.BadParameter("expected 2 parts in %q", id.HostUUID)
}
return parts[0], nil
} | go | func (id *IdentityID) HostID() (string, error) {
parts := strings.Split(id.HostUUID, ".")
if len(parts) < 2 {
return "", trace.BadParameter("expected 2 parts in %q", id.HostUUID)
}
return parts[0], nil
} | [
"func",
"(",
"id",
"*",
"IdentityID",
")",
"HostID",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"parts",
":=",
"strings",
".",
"Split",
"(",
"id",
".",
"HostUUID",
",",
"\"",
"\"",
")",
"\n",
"if",
"len",
"(",
"parts",
")",
"<",
"2",
"{",
"return",
"\"",
"\"",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"id",
".",
"HostUUID",
")",
"\n",
"}",
"\n",
"return",
"parts",
"[",
"0",
"]",
",",
"nil",
"\n",
"}"
] | // HostID is host ID part of the host UUID that consists cluster name | [
"HostID",
"is",
"host",
"ID",
"part",
"of",
"the",
"host",
"UUID",
"that",
"consists",
"cluster",
"name"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/auth/init.go#L677-L683 | train |
gravitational/teleport | lib/auth/init.go | Equals | func (id *IdentityID) Equals(other IdentityID) bool {
return id.Role == other.Role && id.HostUUID == other.HostUUID
} | go | func (id *IdentityID) Equals(other IdentityID) bool {
return id.Role == other.Role && id.HostUUID == other.HostUUID
} | [
"func",
"(",
"id",
"*",
"IdentityID",
")",
"Equals",
"(",
"other",
"IdentityID",
")",
"bool",
"{",
"return",
"id",
".",
"Role",
"==",
"other",
".",
"Role",
"&&",
"id",
".",
"HostUUID",
"==",
"other",
".",
"HostUUID",
"\n",
"}"
] | // Equals returns true if two identities are equal | [
"Equals",
"returns",
"true",
"if",
"two",
"identities",
"are",
"equal"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/auth/init.go#L686-L688 | train |
gravitational/teleport | lib/auth/init.go | ReadIdentityFromKeyPair | func ReadIdentityFromKeyPair(keys *PackedKeys) (*Identity, error) {
identity, err := ReadSSHIdentityFromKeyPair(keys.Key, keys.Cert)
if err != nil {
return nil, trace.Wrap(err)
}
if len(keys.SSHCACerts) != 0 {
identity.SSHCACertBytes = keys.SSHCACerts
}
if len(keys.TLSCACerts) != 0 {
// Parse the key pair to verify that identity parses properly for future use.
i, err := ReadTLSIdentityFromKeyPair(keys.Key, keys.TLSCert, keys.TLSCACerts)
if err != nil {
return nil, trace.Wrap(err)
}
identity.XCert = i.XCert
identity.TLSCertBytes = keys.TLSCert
identity.TLSCACertsBytes = keys.TLSCACerts
}
return identity, nil
} | go | func ReadIdentityFromKeyPair(keys *PackedKeys) (*Identity, error) {
identity, err := ReadSSHIdentityFromKeyPair(keys.Key, keys.Cert)
if err != nil {
return nil, trace.Wrap(err)
}
if len(keys.SSHCACerts) != 0 {
identity.SSHCACertBytes = keys.SSHCACerts
}
if len(keys.TLSCACerts) != 0 {
// Parse the key pair to verify that identity parses properly for future use.
i, err := ReadTLSIdentityFromKeyPair(keys.Key, keys.TLSCert, keys.TLSCACerts)
if err != nil {
return nil, trace.Wrap(err)
}
identity.XCert = i.XCert
identity.TLSCertBytes = keys.TLSCert
identity.TLSCACertsBytes = keys.TLSCACerts
}
return identity, nil
} | [
"func",
"ReadIdentityFromKeyPair",
"(",
"keys",
"*",
"PackedKeys",
")",
"(",
"*",
"Identity",
",",
"error",
")",
"{",
"identity",
",",
"err",
":=",
"ReadSSHIdentityFromKeyPair",
"(",
"keys",
".",
"Key",
",",
"keys",
".",
"Cert",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"if",
"len",
"(",
"keys",
".",
"SSHCACerts",
")",
"!=",
"0",
"{",
"identity",
".",
"SSHCACertBytes",
"=",
"keys",
".",
"SSHCACerts",
"\n",
"}",
"\n\n",
"if",
"len",
"(",
"keys",
".",
"TLSCACerts",
")",
"!=",
"0",
"{",
"// Parse the key pair to verify that identity parses properly for future use.",
"i",
",",
"err",
":=",
"ReadTLSIdentityFromKeyPair",
"(",
"keys",
".",
"Key",
",",
"keys",
".",
"TLSCert",
",",
"keys",
".",
"TLSCACerts",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"identity",
".",
"XCert",
"=",
"i",
".",
"XCert",
"\n",
"identity",
".",
"TLSCertBytes",
"=",
"keys",
".",
"TLSCert",
"\n",
"identity",
".",
"TLSCACertsBytes",
"=",
"keys",
".",
"TLSCACerts",
"\n",
"}",
"\n\n",
"return",
"identity",
",",
"nil",
"\n",
"}"
] | // ReadIdentityFromKeyPair reads SSH and TLS identity from key pair. | [
"ReadIdentityFromKeyPair",
"reads",
"SSH",
"and",
"TLS",
"identity",
"from",
"key",
"pair",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/auth/init.go#L696-L718 | train |
gravitational/teleport | lib/auth/init.go | ReadTLSIdentityFromKeyPair | func ReadTLSIdentityFromKeyPair(keyBytes, certBytes []byte, caCertsBytes [][]byte) (*Identity, error) {
if len(keyBytes) == 0 {
return nil, trace.BadParameter("missing private key")
}
if len(certBytes) == 0 {
return nil, trace.BadParameter("missing certificate")
}
cert, err := tlsca.ParseCertificatePEM(certBytes)
if err != nil {
return nil, trace.Wrap(err, "failed to parse TLS certificate")
}
id, err := tlsca.FromSubject(cert.Subject)
if err != nil {
return nil, trace.Wrap(err)
}
if len(cert.Issuer.Organization) == 0 {
return nil, trace.BadParameter("missing CA organization")
}
clusterName := cert.Issuer.Organization[0]
if clusterName == "" {
return nil, trace.BadParameter("misssing cluster name")
}
identity := &Identity{
ID: IdentityID{HostUUID: id.Username, Role: teleport.Role(id.Groups[0])},
ClusterName: clusterName,
KeyBytes: keyBytes,
TLSCertBytes: certBytes,
TLSCACertsBytes: caCertsBytes,
XCert: cert,
}
// The passed in ciphersuites don't appear to matter here since the returned
// *tls.Config is never actually used?
_, err = identity.TLSConfig(utils.DefaultCipherSuites())
if err != nil {
return nil, trace.Wrap(err)
}
return identity, nil
} | go | func ReadTLSIdentityFromKeyPair(keyBytes, certBytes []byte, caCertsBytes [][]byte) (*Identity, error) {
if len(keyBytes) == 0 {
return nil, trace.BadParameter("missing private key")
}
if len(certBytes) == 0 {
return nil, trace.BadParameter("missing certificate")
}
cert, err := tlsca.ParseCertificatePEM(certBytes)
if err != nil {
return nil, trace.Wrap(err, "failed to parse TLS certificate")
}
id, err := tlsca.FromSubject(cert.Subject)
if err != nil {
return nil, trace.Wrap(err)
}
if len(cert.Issuer.Organization) == 0 {
return nil, trace.BadParameter("missing CA organization")
}
clusterName := cert.Issuer.Organization[0]
if clusterName == "" {
return nil, trace.BadParameter("misssing cluster name")
}
identity := &Identity{
ID: IdentityID{HostUUID: id.Username, Role: teleport.Role(id.Groups[0])},
ClusterName: clusterName,
KeyBytes: keyBytes,
TLSCertBytes: certBytes,
TLSCACertsBytes: caCertsBytes,
XCert: cert,
}
// The passed in ciphersuites don't appear to matter here since the returned
// *tls.Config is never actually used?
_, err = identity.TLSConfig(utils.DefaultCipherSuites())
if err != nil {
return nil, trace.Wrap(err)
}
return identity, nil
} | [
"func",
"ReadTLSIdentityFromKeyPair",
"(",
"keyBytes",
",",
"certBytes",
"[",
"]",
"byte",
",",
"caCertsBytes",
"[",
"]",
"[",
"]",
"byte",
")",
"(",
"*",
"Identity",
",",
"error",
")",
"{",
"if",
"len",
"(",
"keyBytes",
")",
"==",
"0",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"if",
"len",
"(",
"certBytes",
")",
"==",
"0",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"cert",
",",
"err",
":=",
"tlsca",
".",
"ParseCertificatePEM",
"(",
"certBytes",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"id",
",",
"err",
":=",
"tlsca",
".",
"FromSubject",
"(",
"cert",
".",
"Subject",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"if",
"len",
"(",
"cert",
".",
"Issuer",
".",
"Organization",
")",
"==",
"0",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"clusterName",
":=",
"cert",
".",
"Issuer",
".",
"Organization",
"[",
"0",
"]",
"\n",
"if",
"clusterName",
"==",
"\"",
"\"",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"identity",
":=",
"&",
"Identity",
"{",
"ID",
":",
"IdentityID",
"{",
"HostUUID",
":",
"id",
".",
"Username",
",",
"Role",
":",
"teleport",
".",
"Role",
"(",
"id",
".",
"Groups",
"[",
"0",
"]",
")",
"}",
",",
"ClusterName",
":",
"clusterName",
",",
"KeyBytes",
":",
"keyBytes",
",",
"TLSCertBytes",
":",
"certBytes",
",",
"TLSCACertsBytes",
":",
"caCertsBytes",
",",
"XCert",
":",
"cert",
",",
"}",
"\n",
"// The passed in ciphersuites don't appear to matter here since the returned",
"// *tls.Config is never actually used?",
"_",
",",
"err",
"=",
"identity",
".",
"TLSConfig",
"(",
"utils",
".",
"DefaultCipherSuites",
"(",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"return",
"identity",
",",
"nil",
"\n",
"}"
] | // ReadTLSIdentityFromKeyPair reads TLS identity from key pair | [
"ReadTLSIdentityFromKeyPair",
"reads",
"TLS",
"identity",
"from",
"key",
"pair"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/auth/init.go#L721-L763 | train |
gravitational/teleport | lib/auth/init.go | ReadSSHIdentityFromKeyPair | func ReadSSHIdentityFromKeyPair(keyBytes, certBytes []byte) (*Identity, error) {
if len(keyBytes) == 0 {
return nil, trace.BadParameter("PrivateKey: missing private key")
}
if len(certBytes) == 0 {
return nil, trace.BadParameter("Cert: missing parameter")
}
pubKey, _, _, _, err := ssh.ParseAuthorizedKey(certBytes)
if err != nil {
return nil, trace.BadParameter("failed to parse server certificate: %v", err)
}
cert, ok := pubKey.(*ssh.Certificate)
if !ok {
return nil, trace.BadParameter("expected ssh.Certificate, got %v", pubKey)
}
signer, err := ssh.ParsePrivateKey(keyBytes)
if err != nil {
return nil, trace.BadParameter("failed to parse private key: %v", err)
}
// this signer authenticates using certificate signed by the cert authority
// not only by the public key
certSigner, err := ssh.NewCertSigner(cert, signer)
if err != nil {
return nil, trace.BadParameter("unsupported private key: %v", err)
}
// check principals on certificate
if len(cert.ValidPrincipals) < 1 {
return nil, trace.BadParameter("valid principals: at least one valid principal is required")
}
for _, validPrincipal := range cert.ValidPrincipals {
if validPrincipal == "" {
return nil, trace.BadParameter("valid principal can not be empty: %q", cert.ValidPrincipals)
}
}
// check permissions on certificate
if len(cert.Permissions.Extensions) == 0 {
return nil, trace.BadParameter("extensions: misssing needed extensions for host roles")
}
roleString := cert.Permissions.Extensions[utils.CertExtensionRole]
if roleString == "" {
return nil, trace.BadParameter("misssing cert extension %v", utils.CertExtensionRole)
}
roles, err := teleport.ParseRoles(roleString)
if err != nil {
return nil, trace.Wrap(err)
}
foundRoles := len(roles)
if foundRoles != 1 {
return nil, trace.Errorf("expected one role per certificate. found %d: '%s'",
foundRoles, roles.String())
}
role := roles[0]
clusterName := cert.Permissions.Extensions[utils.CertExtensionAuthority]
if clusterName == "" {
return nil, trace.BadParameter("missing cert extension %v", utils.CertExtensionAuthority)
}
return &Identity{
ID: IdentityID{HostUUID: cert.ValidPrincipals[0], Role: role},
ClusterName: clusterName,
KeyBytes: keyBytes,
CertBytes: certBytes,
KeySigner: certSigner,
Cert: cert,
}, nil
} | go | func ReadSSHIdentityFromKeyPair(keyBytes, certBytes []byte) (*Identity, error) {
if len(keyBytes) == 0 {
return nil, trace.BadParameter("PrivateKey: missing private key")
}
if len(certBytes) == 0 {
return nil, trace.BadParameter("Cert: missing parameter")
}
pubKey, _, _, _, err := ssh.ParseAuthorizedKey(certBytes)
if err != nil {
return nil, trace.BadParameter("failed to parse server certificate: %v", err)
}
cert, ok := pubKey.(*ssh.Certificate)
if !ok {
return nil, trace.BadParameter("expected ssh.Certificate, got %v", pubKey)
}
signer, err := ssh.ParsePrivateKey(keyBytes)
if err != nil {
return nil, trace.BadParameter("failed to parse private key: %v", err)
}
// this signer authenticates using certificate signed by the cert authority
// not only by the public key
certSigner, err := ssh.NewCertSigner(cert, signer)
if err != nil {
return nil, trace.BadParameter("unsupported private key: %v", err)
}
// check principals on certificate
if len(cert.ValidPrincipals) < 1 {
return nil, trace.BadParameter("valid principals: at least one valid principal is required")
}
for _, validPrincipal := range cert.ValidPrincipals {
if validPrincipal == "" {
return nil, trace.BadParameter("valid principal can not be empty: %q", cert.ValidPrincipals)
}
}
// check permissions on certificate
if len(cert.Permissions.Extensions) == 0 {
return nil, trace.BadParameter("extensions: misssing needed extensions for host roles")
}
roleString := cert.Permissions.Extensions[utils.CertExtensionRole]
if roleString == "" {
return nil, trace.BadParameter("misssing cert extension %v", utils.CertExtensionRole)
}
roles, err := teleport.ParseRoles(roleString)
if err != nil {
return nil, trace.Wrap(err)
}
foundRoles := len(roles)
if foundRoles != 1 {
return nil, trace.Errorf("expected one role per certificate. found %d: '%s'",
foundRoles, roles.String())
}
role := roles[0]
clusterName := cert.Permissions.Extensions[utils.CertExtensionAuthority]
if clusterName == "" {
return nil, trace.BadParameter("missing cert extension %v", utils.CertExtensionAuthority)
}
return &Identity{
ID: IdentityID{HostUUID: cert.ValidPrincipals[0], Role: role},
ClusterName: clusterName,
KeyBytes: keyBytes,
CertBytes: certBytes,
KeySigner: certSigner,
Cert: cert,
}, nil
} | [
"func",
"ReadSSHIdentityFromKeyPair",
"(",
"keyBytes",
",",
"certBytes",
"[",
"]",
"byte",
")",
"(",
"*",
"Identity",
",",
"error",
")",
"{",
"if",
"len",
"(",
"keyBytes",
")",
"==",
"0",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"if",
"len",
"(",
"certBytes",
")",
"==",
"0",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"pubKey",
",",
"_",
",",
"_",
",",
"_",
",",
"err",
":=",
"ssh",
".",
"ParseAuthorizedKey",
"(",
"certBytes",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n\n",
"cert",
",",
"ok",
":=",
"pubKey",
".",
"(",
"*",
"ssh",
".",
"Certificate",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"pubKey",
")",
"\n",
"}",
"\n\n",
"signer",
",",
"err",
":=",
"ssh",
".",
"ParsePrivateKey",
"(",
"keyBytes",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n",
"// this signer authenticates using certificate signed by the cert authority",
"// not only by the public key",
"certSigner",
",",
"err",
":=",
"ssh",
".",
"NewCertSigner",
"(",
"cert",
",",
"signer",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n\n",
"// check principals on certificate",
"if",
"len",
"(",
"cert",
".",
"ValidPrincipals",
")",
"<",
"1",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"for",
"_",
",",
"validPrincipal",
":=",
"range",
"cert",
".",
"ValidPrincipals",
"{",
"if",
"validPrincipal",
"==",
"\"",
"\"",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"cert",
".",
"ValidPrincipals",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"// check permissions on certificate",
"if",
"len",
"(",
"cert",
".",
"Permissions",
".",
"Extensions",
")",
"==",
"0",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"roleString",
":=",
"cert",
".",
"Permissions",
".",
"Extensions",
"[",
"utils",
".",
"CertExtensionRole",
"]",
"\n",
"if",
"roleString",
"==",
"\"",
"\"",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"utils",
".",
"CertExtensionRole",
")",
"\n",
"}",
"\n",
"roles",
",",
"err",
":=",
"teleport",
".",
"ParseRoles",
"(",
"roleString",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"foundRoles",
":=",
"len",
"(",
"roles",
")",
"\n",
"if",
"foundRoles",
"!=",
"1",
"{",
"return",
"nil",
",",
"trace",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"foundRoles",
",",
"roles",
".",
"String",
"(",
")",
")",
"\n",
"}",
"\n",
"role",
":=",
"roles",
"[",
"0",
"]",
"\n",
"clusterName",
":=",
"cert",
".",
"Permissions",
".",
"Extensions",
"[",
"utils",
".",
"CertExtensionAuthority",
"]",
"\n",
"if",
"clusterName",
"==",
"\"",
"\"",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"utils",
".",
"CertExtensionAuthority",
")",
"\n",
"}",
"\n\n",
"return",
"&",
"Identity",
"{",
"ID",
":",
"IdentityID",
"{",
"HostUUID",
":",
"cert",
".",
"ValidPrincipals",
"[",
"0",
"]",
",",
"Role",
":",
"role",
"}",
",",
"ClusterName",
":",
"clusterName",
",",
"KeyBytes",
":",
"keyBytes",
",",
"CertBytes",
":",
"certBytes",
",",
"KeySigner",
":",
"certSigner",
",",
"Cert",
":",
"cert",
",",
"}",
",",
"nil",
"\n",
"}"
] | // ReadSSHIdentityFromKeyPair reads identity from initialized keypair | [
"ReadSSHIdentityFromKeyPair",
"reads",
"identity",
"from",
"initialized",
"keypair"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/auth/init.go#L766-L837 | train |
gravitational/teleport | lib/services/role.go | NewImplicitRole | func NewImplicitRole() Role {
return &RoleV3{
Kind: KindRole,
Version: V3,
Metadata: Metadata{
Name: teleport.DefaultImplicitRole,
Namespace: defaults.Namespace,
},
Spec: RoleSpecV3{
Options: RoleOptions{
MaxSessionTTL: MaxDuration(),
},
Allow: RoleConditions{
Namespaces: []string{defaults.Namespace},
Rules: CopyRulesSlice(DefaultImplicitRules),
},
},
}
} | go | func NewImplicitRole() Role {
return &RoleV3{
Kind: KindRole,
Version: V3,
Metadata: Metadata{
Name: teleport.DefaultImplicitRole,
Namespace: defaults.Namespace,
},
Spec: RoleSpecV3{
Options: RoleOptions{
MaxSessionTTL: MaxDuration(),
},
Allow: RoleConditions{
Namespaces: []string{defaults.Namespace},
Rules: CopyRulesSlice(DefaultImplicitRules),
},
},
}
} | [
"func",
"NewImplicitRole",
"(",
")",
"Role",
"{",
"return",
"&",
"RoleV3",
"{",
"Kind",
":",
"KindRole",
",",
"Version",
":",
"V3",
",",
"Metadata",
":",
"Metadata",
"{",
"Name",
":",
"teleport",
".",
"DefaultImplicitRole",
",",
"Namespace",
":",
"defaults",
".",
"Namespace",
",",
"}",
",",
"Spec",
":",
"RoleSpecV3",
"{",
"Options",
":",
"RoleOptions",
"{",
"MaxSessionTTL",
":",
"MaxDuration",
"(",
")",
",",
"}",
",",
"Allow",
":",
"RoleConditions",
"{",
"Namespaces",
":",
"[",
"]",
"string",
"{",
"defaults",
".",
"Namespace",
"}",
",",
"Rules",
":",
"CopyRulesSlice",
"(",
"DefaultImplicitRules",
")",
",",
"}",
",",
"}",
",",
"}",
"\n",
"}"
] | // NewImplicitRole is the default implicit role that gets added to all
// RoleSets. | [
"NewImplicitRole",
"is",
"the",
"default",
"implicit",
"role",
"that",
"gets",
"added",
"to",
"all",
"RoleSets",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L115-L133 | train |
gravitational/teleport | lib/services/role.go | RoleForUser | func RoleForUser(u User) Role {
return &RoleV3{
Kind: KindRole,
Version: V3,
Metadata: Metadata{
Name: RoleNameForUser(u.GetName()),
Namespace: defaults.Namespace,
},
Spec: RoleSpecV3{
Options: RoleOptions{
CertificateFormat: teleport.CertificateFormatStandard,
MaxSessionTTL: NewDuration(defaults.MaxCertDuration),
PortForwarding: NewBoolOption(true),
ForwardAgent: NewBool(true),
},
Allow: RoleConditions{
Namespaces: []string{defaults.Namespace},
NodeLabels: Labels{Wildcard: []string{Wildcard}},
Rules: CopyRulesSlice(AdminUserRules),
},
},
}
} | go | func RoleForUser(u User) Role {
return &RoleV3{
Kind: KindRole,
Version: V3,
Metadata: Metadata{
Name: RoleNameForUser(u.GetName()),
Namespace: defaults.Namespace,
},
Spec: RoleSpecV3{
Options: RoleOptions{
CertificateFormat: teleport.CertificateFormatStandard,
MaxSessionTTL: NewDuration(defaults.MaxCertDuration),
PortForwarding: NewBoolOption(true),
ForwardAgent: NewBool(true),
},
Allow: RoleConditions{
Namespaces: []string{defaults.Namespace},
NodeLabels: Labels{Wildcard: []string{Wildcard}},
Rules: CopyRulesSlice(AdminUserRules),
},
},
}
} | [
"func",
"RoleForUser",
"(",
"u",
"User",
")",
"Role",
"{",
"return",
"&",
"RoleV3",
"{",
"Kind",
":",
"KindRole",
",",
"Version",
":",
"V3",
",",
"Metadata",
":",
"Metadata",
"{",
"Name",
":",
"RoleNameForUser",
"(",
"u",
".",
"GetName",
"(",
")",
")",
",",
"Namespace",
":",
"defaults",
".",
"Namespace",
",",
"}",
",",
"Spec",
":",
"RoleSpecV3",
"{",
"Options",
":",
"RoleOptions",
"{",
"CertificateFormat",
":",
"teleport",
".",
"CertificateFormatStandard",
",",
"MaxSessionTTL",
":",
"NewDuration",
"(",
"defaults",
".",
"MaxCertDuration",
")",
",",
"PortForwarding",
":",
"NewBoolOption",
"(",
"true",
")",
",",
"ForwardAgent",
":",
"NewBool",
"(",
"true",
")",
",",
"}",
",",
"Allow",
":",
"RoleConditions",
"{",
"Namespaces",
":",
"[",
"]",
"string",
"{",
"defaults",
".",
"Namespace",
"}",
",",
"NodeLabels",
":",
"Labels",
"{",
"Wildcard",
":",
"[",
"]",
"string",
"{",
"Wildcard",
"}",
"}",
",",
"Rules",
":",
"CopyRulesSlice",
"(",
"AdminUserRules",
")",
",",
"}",
",",
"}",
",",
"}",
"\n",
"}"
] | // RoleForUser creates an admin role for a services.User. | [
"RoleForUser",
"creates",
"an",
"admin",
"role",
"for",
"a",
"services",
".",
"User",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L136-L158 | train |
gravitational/teleport | lib/services/role.go | RoleForCertAuthority | func RoleForCertAuthority(ca CertAuthority) Role {
return &RoleV3{
Kind: KindRole,
Version: V3,
Metadata: Metadata{
Name: RoleNameForCertAuthority(ca.GetClusterName()),
Namespace: defaults.Namespace,
},
Spec: RoleSpecV3{
Options: RoleOptions{
MaxSessionTTL: NewDuration(defaults.MaxCertDuration),
},
Allow: RoleConditions{
Namespaces: []string{defaults.Namespace},
NodeLabels: Labels{Wildcard: []string{Wildcard}},
Rules: CopyRulesSlice(DefaultCertAuthorityRules),
},
},
}
} | go | func RoleForCertAuthority(ca CertAuthority) Role {
return &RoleV3{
Kind: KindRole,
Version: V3,
Metadata: Metadata{
Name: RoleNameForCertAuthority(ca.GetClusterName()),
Namespace: defaults.Namespace,
},
Spec: RoleSpecV3{
Options: RoleOptions{
MaxSessionTTL: NewDuration(defaults.MaxCertDuration),
},
Allow: RoleConditions{
Namespaces: []string{defaults.Namespace},
NodeLabels: Labels{Wildcard: []string{Wildcard}},
Rules: CopyRulesSlice(DefaultCertAuthorityRules),
},
},
}
} | [
"func",
"RoleForCertAuthority",
"(",
"ca",
"CertAuthority",
")",
"Role",
"{",
"return",
"&",
"RoleV3",
"{",
"Kind",
":",
"KindRole",
",",
"Version",
":",
"V3",
",",
"Metadata",
":",
"Metadata",
"{",
"Name",
":",
"RoleNameForCertAuthority",
"(",
"ca",
".",
"GetClusterName",
"(",
")",
")",
",",
"Namespace",
":",
"defaults",
".",
"Namespace",
",",
"}",
",",
"Spec",
":",
"RoleSpecV3",
"{",
"Options",
":",
"RoleOptions",
"{",
"MaxSessionTTL",
":",
"NewDuration",
"(",
"defaults",
".",
"MaxCertDuration",
")",
",",
"}",
",",
"Allow",
":",
"RoleConditions",
"{",
"Namespaces",
":",
"[",
"]",
"string",
"{",
"defaults",
".",
"Namespace",
"}",
",",
"NodeLabels",
":",
"Labels",
"{",
"Wildcard",
":",
"[",
"]",
"string",
"{",
"Wildcard",
"}",
"}",
",",
"Rules",
":",
"CopyRulesSlice",
"(",
"DefaultCertAuthorityRules",
")",
",",
"}",
",",
"}",
",",
"}",
"\n",
"}"
] | // RoleForCertauthority creates role using services.CertAuthority. | [
"RoleForCertauthority",
"creates",
"role",
"using",
"services",
".",
"CertAuthority",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L161-L180 | train |
gravitational/teleport | lib/services/role.go | ConvertV1CertAuthority | func ConvertV1CertAuthority(v1 *CertAuthorityV1) (CertAuthority, Role) {
ca := v1.V2()
role := RoleForCertAuthority(ca)
role.SetLogins(Allow, v1.AllowedLogins)
ca.AddRole(role.GetName())
return ca, role
} | go | func ConvertV1CertAuthority(v1 *CertAuthorityV1) (CertAuthority, Role) {
ca := v1.V2()
role := RoleForCertAuthority(ca)
role.SetLogins(Allow, v1.AllowedLogins)
ca.AddRole(role.GetName())
return ca, role
} | [
"func",
"ConvertV1CertAuthority",
"(",
"v1",
"*",
"CertAuthorityV1",
")",
"(",
"CertAuthority",
",",
"Role",
")",
"{",
"ca",
":=",
"v1",
".",
"V2",
"(",
")",
"\n",
"role",
":=",
"RoleForCertAuthority",
"(",
"ca",
")",
"\n",
"role",
".",
"SetLogins",
"(",
"Allow",
",",
"v1",
".",
"AllowedLogins",
")",
"\n",
"ca",
".",
"AddRole",
"(",
"role",
".",
"GetName",
"(",
")",
")",
"\n",
"return",
"ca",
",",
"role",
"\n",
"}"
] | // ConvertV1CertAuthority converts V1 cert authority for new CA and Role | [
"ConvertV1CertAuthority",
"converts",
"V1",
"cert",
"authority",
"for",
"new",
"CA",
"and",
"Role"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L183-L189 | train |
gravitational/teleport | lib/services/role.go | applyValueTraits | func applyValueTraits(val string, traits map[string][]string) ([]string, error) {
// Extract the variablePrefix and variableName from the role variable.
variablePrefix, variableName, err := parse.IsRoleVariable(val)
if err != nil {
if !trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
return []string{val}, nil
}
// For internal traits, only internal.logins and internal.kubernetes_groups is supported at the moment.
if variablePrefix == teleport.TraitInternalPrefix {
if variableName != teleport.TraitLogins && variableName != teleport.TraitKubeGroups {
return nil, trace.BadParameter("unsupported variable %q", variableName)
}
}
// If the variable is not found in the traits, skip it.
variableValues, ok := traits[variableName]
if !ok || len(variableValues) == 0 {
return nil, trace.NotFound("variable %q not found in traits", variableName)
}
return append([]string{}, variableValues...), nil
} | go | func applyValueTraits(val string, traits map[string][]string) ([]string, error) {
// Extract the variablePrefix and variableName from the role variable.
variablePrefix, variableName, err := parse.IsRoleVariable(val)
if err != nil {
if !trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
return []string{val}, nil
}
// For internal traits, only internal.logins and internal.kubernetes_groups is supported at the moment.
if variablePrefix == teleport.TraitInternalPrefix {
if variableName != teleport.TraitLogins && variableName != teleport.TraitKubeGroups {
return nil, trace.BadParameter("unsupported variable %q", variableName)
}
}
// If the variable is not found in the traits, skip it.
variableValues, ok := traits[variableName]
if !ok || len(variableValues) == 0 {
return nil, trace.NotFound("variable %q not found in traits", variableName)
}
return append([]string{}, variableValues...), nil
} | [
"func",
"applyValueTraits",
"(",
"val",
"string",
",",
"traits",
"map",
"[",
"string",
"]",
"[",
"]",
"string",
")",
"(",
"[",
"]",
"string",
",",
"error",
")",
"{",
"// Extract the variablePrefix and variableName from the role variable.",
"variablePrefix",
",",
"variableName",
",",
"err",
":=",
"parse",
".",
"IsRoleVariable",
"(",
"val",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"if",
"!",
"trace",
".",
"IsNotFound",
"(",
"err",
")",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"return",
"[",
"]",
"string",
"{",
"val",
"}",
",",
"nil",
"\n",
"}",
"\n\n",
"// For internal traits, only internal.logins and internal.kubernetes_groups is supported at the moment.",
"if",
"variablePrefix",
"==",
"teleport",
".",
"TraitInternalPrefix",
"{",
"if",
"variableName",
"!=",
"teleport",
".",
"TraitLogins",
"&&",
"variableName",
"!=",
"teleport",
".",
"TraitKubeGroups",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"variableName",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"// If the variable is not found in the traits, skip it.",
"variableValues",
",",
"ok",
":=",
"traits",
"[",
"variableName",
"]",
"\n",
"if",
"!",
"ok",
"||",
"len",
"(",
"variableValues",
")",
"==",
"0",
"{",
"return",
"nil",
",",
"trace",
".",
"NotFound",
"(",
"\"",
"\"",
",",
"variableName",
")",
"\n",
"}",
"\n\n",
"return",
"append",
"(",
"[",
"]",
"string",
"{",
"}",
",",
"variableValues",
"...",
")",
",",
"nil",
"\n",
"}"
] | // applyValueTraits applies the passed in traits to the variable,
// returns BadParameter in case if referenced variable is unsupported,
// returns NotFound in case if referenced trait is missing,
// mapped list of values otherwise, the function guarantees to return
// at least one value in case if return value is nil | [
"applyValueTraits",
"applies",
"the",
"passed",
"in",
"traits",
"to",
"the",
"variable",
"returns",
"BadParameter",
"in",
"case",
"if",
"referenced",
"variable",
"is",
"unsupported",
"returns",
"NotFound",
"in",
"case",
"if",
"referenced",
"trait",
"is",
"missing",
"mapped",
"list",
"of",
"values",
"otherwise",
"the",
"function",
"guarantees",
"to",
"return",
"at",
"least",
"one",
"value",
"in",
"case",
"if",
"return",
"value",
"is",
"nil"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L349-L374 | train |
gravitational/teleport | lib/services/role.go | Equals | func (r *RoleV3) Equals(other Role) bool {
if !r.GetOptions().Equals(other.GetOptions()) {
return false
}
for _, condition := range []RoleConditionType{Allow, Deny} {
if !utils.StringSlicesEqual(r.GetLogins(condition), other.GetLogins(condition)) {
return false
}
if !utils.StringSlicesEqual(r.GetNamespaces(condition), other.GetNamespaces(condition)) {
return false
}
if !r.GetNodeLabels(condition).Equals(other.GetNodeLabels(condition)) {
return false
}
if !RuleSlicesEqual(r.GetRules(condition), other.GetRules(condition)) {
return false
}
}
return true
} | go | func (r *RoleV3) Equals(other Role) bool {
if !r.GetOptions().Equals(other.GetOptions()) {
return false
}
for _, condition := range []RoleConditionType{Allow, Deny} {
if !utils.StringSlicesEqual(r.GetLogins(condition), other.GetLogins(condition)) {
return false
}
if !utils.StringSlicesEqual(r.GetNamespaces(condition), other.GetNamespaces(condition)) {
return false
}
if !r.GetNodeLabels(condition).Equals(other.GetNodeLabels(condition)) {
return false
}
if !RuleSlicesEqual(r.GetRules(condition), other.GetRules(condition)) {
return false
}
}
return true
} | [
"func",
"(",
"r",
"*",
"RoleV3",
")",
"Equals",
"(",
"other",
"Role",
")",
"bool",
"{",
"if",
"!",
"r",
".",
"GetOptions",
"(",
")",
".",
"Equals",
"(",
"other",
".",
"GetOptions",
"(",
")",
")",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"condition",
":=",
"range",
"[",
"]",
"RoleConditionType",
"{",
"Allow",
",",
"Deny",
"}",
"{",
"if",
"!",
"utils",
".",
"StringSlicesEqual",
"(",
"r",
".",
"GetLogins",
"(",
"condition",
")",
",",
"other",
".",
"GetLogins",
"(",
"condition",
")",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"if",
"!",
"utils",
".",
"StringSlicesEqual",
"(",
"r",
".",
"GetNamespaces",
"(",
"condition",
")",
",",
"other",
".",
"GetNamespaces",
"(",
"condition",
")",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"if",
"!",
"r",
".",
"GetNodeLabels",
"(",
"condition",
")",
".",
"Equals",
"(",
"other",
".",
"GetNodeLabels",
"(",
"condition",
")",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"if",
"!",
"RuleSlicesEqual",
"(",
"r",
".",
"GetRules",
"(",
"condition",
")",
",",
"other",
".",
"GetRules",
"(",
"condition",
")",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"true",
"\n",
"}"
] | // Equals returns true if the roles are equal. Roles are equal if options,
// namespaces, logins, labels, and conditions match. | [
"Equals",
"returns",
"true",
"if",
"the",
"roles",
"are",
"equal",
".",
"Roles",
"are",
"equal",
"if",
"options",
"namespaces",
"logins",
"labels",
"and",
"conditions",
"match",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L408-L429 | train |
gravitational/teleport | lib/services/role.go | GetLogins | func (r *RoleV3) GetLogins(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.Logins
}
return r.Spec.Deny.Logins
} | go | func (r *RoleV3) GetLogins(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.Logins
}
return r.Spec.Deny.Logins
} | [
"func",
"(",
"r",
"*",
"RoleV3",
")",
"GetLogins",
"(",
"rct",
"RoleConditionType",
")",
"[",
"]",
"string",
"{",
"if",
"rct",
"==",
"Allow",
"{",
"return",
"r",
".",
"Spec",
".",
"Allow",
".",
"Logins",
"\n",
"}",
"\n",
"return",
"r",
".",
"Spec",
".",
"Deny",
".",
"Logins",
"\n",
"}"
] | // GetLogins gets system logins for allow or deny condition. | [
"GetLogins",
"gets",
"system",
"logins",
"for",
"allow",
"or",
"deny",
"condition",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L478-L483 | train |
gravitational/teleport | lib/services/role.go | SetLogins | func (r *RoleV3) SetLogins(rct RoleConditionType, logins []string) {
lcopy := utils.CopyStrings(logins)
if rct == Allow {
r.Spec.Allow.Logins = lcopy
} else {
r.Spec.Deny.Logins = lcopy
}
} | go | func (r *RoleV3) SetLogins(rct RoleConditionType, logins []string) {
lcopy := utils.CopyStrings(logins)
if rct == Allow {
r.Spec.Allow.Logins = lcopy
} else {
r.Spec.Deny.Logins = lcopy
}
} | [
"func",
"(",
"r",
"*",
"RoleV3",
")",
"SetLogins",
"(",
"rct",
"RoleConditionType",
",",
"logins",
"[",
"]",
"string",
")",
"{",
"lcopy",
":=",
"utils",
".",
"CopyStrings",
"(",
"logins",
")",
"\n\n",
"if",
"rct",
"==",
"Allow",
"{",
"r",
".",
"Spec",
".",
"Allow",
".",
"Logins",
"=",
"lcopy",
"\n",
"}",
"else",
"{",
"r",
".",
"Spec",
".",
"Deny",
".",
"Logins",
"=",
"lcopy",
"\n",
"}",
"\n",
"}"
] | // SetLogins sets system logins for allow or deny condition. | [
"SetLogins",
"sets",
"system",
"logins",
"for",
"allow",
"or",
"deny",
"condition",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L486-L494 | train |
gravitational/teleport | lib/services/role.go | GetKubeGroups | func (r *RoleV3) GetKubeGroups(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.KubeGroups
}
return r.Spec.Deny.KubeGroups
} | go | func (r *RoleV3) GetKubeGroups(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.KubeGroups
}
return r.Spec.Deny.KubeGroups
} | [
"func",
"(",
"r",
"*",
"RoleV3",
")",
"GetKubeGroups",
"(",
"rct",
"RoleConditionType",
")",
"[",
"]",
"string",
"{",
"if",
"rct",
"==",
"Allow",
"{",
"return",
"r",
".",
"Spec",
".",
"Allow",
".",
"KubeGroups",
"\n",
"}",
"\n",
"return",
"r",
".",
"Spec",
".",
"Deny",
".",
"KubeGroups",
"\n",
"}"
] | // GetKubeGroups returns kubernetes groups | [
"GetKubeGroups",
"returns",
"kubernetes",
"groups"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L497-L502 | train |
gravitational/teleport | lib/services/role.go | SetKubeGroups | func (r *RoleV3) SetKubeGroups(rct RoleConditionType, groups []string) {
lcopy := utils.CopyStrings(groups)
if rct == Allow {
r.Spec.Allow.KubeGroups = lcopy
} else {
r.Spec.Deny.KubeGroups = lcopy
}
} | go | func (r *RoleV3) SetKubeGroups(rct RoleConditionType, groups []string) {
lcopy := utils.CopyStrings(groups)
if rct == Allow {
r.Spec.Allow.KubeGroups = lcopy
} else {
r.Spec.Deny.KubeGroups = lcopy
}
} | [
"func",
"(",
"r",
"*",
"RoleV3",
")",
"SetKubeGroups",
"(",
"rct",
"RoleConditionType",
",",
"groups",
"[",
"]",
"string",
")",
"{",
"lcopy",
":=",
"utils",
".",
"CopyStrings",
"(",
"groups",
")",
"\n\n",
"if",
"rct",
"==",
"Allow",
"{",
"r",
".",
"Spec",
".",
"Allow",
".",
"KubeGroups",
"=",
"lcopy",
"\n",
"}",
"else",
"{",
"r",
".",
"Spec",
".",
"Deny",
".",
"KubeGroups",
"=",
"lcopy",
"\n",
"}",
"\n",
"}"
] | // SetKubeGroups sets kubernetes groups for allow or deny condition. | [
"SetKubeGroups",
"sets",
"kubernetes",
"groups",
"for",
"allow",
"or",
"deny",
"condition",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L505-L513 | train |
gravitational/teleport | lib/services/role.go | GetNamespaces | func (r *RoleV3) GetNamespaces(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.Namespaces
}
return r.Spec.Deny.Namespaces
} | go | func (r *RoleV3) GetNamespaces(rct RoleConditionType) []string {
if rct == Allow {
return r.Spec.Allow.Namespaces
}
return r.Spec.Deny.Namespaces
} | [
"func",
"(",
"r",
"*",
"RoleV3",
")",
"GetNamespaces",
"(",
"rct",
"RoleConditionType",
")",
"[",
"]",
"string",
"{",
"if",
"rct",
"==",
"Allow",
"{",
"return",
"r",
".",
"Spec",
".",
"Allow",
".",
"Namespaces",
"\n",
"}",
"\n",
"return",
"r",
".",
"Spec",
".",
"Deny",
".",
"Namespaces",
"\n",
"}"
] | // GetNamespaces gets a list of namespaces this role is allowed or denied access to. | [
"GetNamespaces",
"gets",
"a",
"list",
"of",
"namespaces",
"this",
"role",
"is",
"allowed",
"or",
"denied",
"access",
"to",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L516-L521 | train |
gravitational/teleport | lib/services/role.go | SetNamespaces | func (r *RoleV3) SetNamespaces(rct RoleConditionType, namespaces []string) {
ncopy := utils.CopyStrings(namespaces)
if rct == Allow {
r.Spec.Allow.Namespaces = ncopy
} else {
r.Spec.Deny.Namespaces = ncopy
}
} | go | func (r *RoleV3) SetNamespaces(rct RoleConditionType, namespaces []string) {
ncopy := utils.CopyStrings(namespaces)
if rct == Allow {
r.Spec.Allow.Namespaces = ncopy
} else {
r.Spec.Deny.Namespaces = ncopy
}
} | [
"func",
"(",
"r",
"*",
"RoleV3",
")",
"SetNamespaces",
"(",
"rct",
"RoleConditionType",
",",
"namespaces",
"[",
"]",
"string",
")",
"{",
"ncopy",
":=",
"utils",
".",
"CopyStrings",
"(",
"namespaces",
")",
"\n\n",
"if",
"rct",
"==",
"Allow",
"{",
"r",
".",
"Spec",
".",
"Allow",
".",
"Namespaces",
"=",
"ncopy",
"\n",
"}",
"else",
"{",
"r",
".",
"Spec",
".",
"Deny",
".",
"Namespaces",
"=",
"ncopy",
"\n",
"}",
"\n",
"}"
] | // GetNamespaces sets a list of namespaces this role is allowed or denied access to. | [
"GetNamespaces",
"sets",
"a",
"list",
"of",
"namespaces",
"this",
"role",
"is",
"allowed",
"or",
"denied",
"access",
"to",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L524-L532 | train |
gravitational/teleport | lib/services/role.go | GetNodeLabels | func (r *RoleV3) GetNodeLabels(rct RoleConditionType) Labels {
if rct == Allow {
return r.Spec.Allow.NodeLabels
}
return r.Spec.Deny.NodeLabels
} | go | func (r *RoleV3) GetNodeLabels(rct RoleConditionType) Labels {
if rct == Allow {
return r.Spec.Allow.NodeLabels
}
return r.Spec.Deny.NodeLabels
} | [
"func",
"(",
"r",
"*",
"RoleV3",
")",
"GetNodeLabels",
"(",
"rct",
"RoleConditionType",
")",
"Labels",
"{",
"if",
"rct",
"==",
"Allow",
"{",
"return",
"r",
".",
"Spec",
".",
"Allow",
".",
"NodeLabels",
"\n",
"}",
"\n",
"return",
"r",
".",
"Spec",
".",
"Deny",
".",
"NodeLabels",
"\n",
"}"
] | // GetNodeLabels gets the map of node labels this role is allowed or denied access to. | [
"GetNodeLabels",
"gets",
"the",
"map",
"of",
"node",
"labels",
"this",
"role",
"is",
"allowed",
"or",
"denied",
"access",
"to",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L535-L540 | train |
gravitational/teleport | lib/services/role.go | SetNodeLabels | func (r *RoleV3) SetNodeLabels(rct RoleConditionType, labels Labels) {
if rct == Allow {
r.Spec.Allow.NodeLabels = labels.Clone()
} else {
r.Spec.Deny.NodeLabels = labels.Clone()
}
} | go | func (r *RoleV3) SetNodeLabels(rct RoleConditionType, labels Labels) {
if rct == Allow {
r.Spec.Allow.NodeLabels = labels.Clone()
} else {
r.Spec.Deny.NodeLabels = labels.Clone()
}
} | [
"func",
"(",
"r",
"*",
"RoleV3",
")",
"SetNodeLabels",
"(",
"rct",
"RoleConditionType",
",",
"labels",
"Labels",
")",
"{",
"if",
"rct",
"==",
"Allow",
"{",
"r",
".",
"Spec",
".",
"Allow",
".",
"NodeLabels",
"=",
"labels",
".",
"Clone",
"(",
")",
"\n",
"}",
"else",
"{",
"r",
".",
"Spec",
".",
"Deny",
".",
"NodeLabels",
"=",
"labels",
".",
"Clone",
"(",
")",
"\n",
"}",
"\n",
"}"
] | // SetNodeLabels sets the map of node labels this role is allowed or denied access to. | [
"SetNodeLabels",
"sets",
"the",
"map",
"of",
"node",
"labels",
"this",
"role",
"is",
"allowed",
"or",
"denied",
"access",
"to",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L543-L549 | train |
gravitational/teleport | lib/services/role.go | GetRules | func (r *RoleV3) GetRules(rct RoleConditionType) []Rule {
if rct == Allow {
return r.Spec.Allow.Rules
}
return r.Spec.Deny.Rules
} | go | func (r *RoleV3) GetRules(rct RoleConditionType) []Rule {
if rct == Allow {
return r.Spec.Allow.Rules
}
return r.Spec.Deny.Rules
} | [
"func",
"(",
"r",
"*",
"RoleV3",
")",
"GetRules",
"(",
"rct",
"RoleConditionType",
")",
"[",
"]",
"Rule",
"{",
"if",
"rct",
"==",
"Allow",
"{",
"return",
"r",
".",
"Spec",
".",
"Allow",
".",
"Rules",
"\n",
"}",
"\n",
"return",
"r",
".",
"Spec",
".",
"Deny",
".",
"Rules",
"\n",
"}"
] | // GetRules gets all allow or deny rules. | [
"GetRules",
"gets",
"all",
"allow",
"or",
"deny",
"rules",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L552-L557 | train |
gravitational/teleport | lib/services/role.go | SetRules | func (r *RoleV3) SetRules(rct RoleConditionType, in []Rule) {
rcopy := CopyRulesSlice(in)
if rct == Allow {
r.Spec.Allow.Rules = rcopy
} else {
r.Spec.Deny.Rules = rcopy
}
} | go | func (r *RoleV3) SetRules(rct RoleConditionType, in []Rule) {
rcopy := CopyRulesSlice(in)
if rct == Allow {
r.Spec.Allow.Rules = rcopy
} else {
r.Spec.Deny.Rules = rcopy
}
} | [
"func",
"(",
"r",
"*",
"RoleV3",
")",
"SetRules",
"(",
"rct",
"RoleConditionType",
",",
"in",
"[",
"]",
"Rule",
")",
"{",
"rcopy",
":=",
"CopyRulesSlice",
"(",
"in",
")",
"\n\n",
"if",
"rct",
"==",
"Allow",
"{",
"r",
".",
"Spec",
".",
"Allow",
".",
"Rules",
"=",
"rcopy",
"\n",
"}",
"else",
"{",
"r",
".",
"Spec",
".",
"Deny",
".",
"Rules",
"=",
"rcopy",
"\n",
"}",
"\n",
"}"
] | // SetRules sets an allow or deny rule. | [
"SetRules",
"sets",
"an",
"allow",
"or",
"deny",
"rule",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L560-L568 | train |
gravitational/teleport | lib/services/role.go | String | func (r *RoleV3) String() string {
return fmt.Sprintf("Role(Name=%v,Options=%v,Allow=%+v,Deny=%+v)",
r.GetName(), r.Spec.Options, r.Spec.Allow, r.Spec.Deny)
} | go | func (r *RoleV3) String() string {
return fmt.Sprintf("Role(Name=%v,Options=%v,Allow=%+v,Deny=%+v)",
r.GetName(), r.Spec.Options, r.Spec.Allow, r.Spec.Deny)
} | [
"func",
"(",
"r",
"*",
"RoleV3",
")",
"String",
"(",
")",
"string",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"r",
".",
"GetName",
"(",
")",
",",
"r",
".",
"Spec",
".",
"Options",
",",
"r",
".",
"Spec",
".",
"Allow",
",",
"r",
".",
"Spec",
".",
"Deny",
")",
"\n",
"}"
] | // String returns the human readable representation of a role. | [
"String",
"returns",
"the",
"human",
"readable",
"representation",
"of",
"a",
"role",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L641-L644 | train |
gravitational/teleport | lib/services/role.go | NewRule | func NewRule(resource string, verbs []string) Rule {
return Rule{
Resources: []string{resource},
Verbs: verbs,
}
} | go | func NewRule(resource string, verbs []string) Rule {
return Rule{
Resources: []string{resource},
Verbs: verbs,
}
} | [
"func",
"NewRule",
"(",
"resource",
"string",
",",
"verbs",
"[",
"]",
"string",
")",
"Rule",
"{",
"return",
"Rule",
"{",
"Resources",
":",
"[",
"]",
"string",
"{",
"resource",
"}",
",",
"Verbs",
":",
"verbs",
",",
"}",
"\n",
"}"
] | // NewRule creates a rule based on a resource name and a list of verbs | [
"NewRule",
"creates",
"a",
"rule",
"based",
"on",
"a",
"resource",
"name",
"and",
"a",
"list",
"of",
"verbs"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L680-L685 | train |
gravitational/teleport | lib/services/role.go | CheckAndSetDefaults | func (r *Rule) CheckAndSetDefaults() error {
if len(r.Resources) == 0 {
return trace.BadParameter("missing resources to match")
}
if len(r.Verbs) == 0 {
return trace.BadParameter("missing verbs")
}
if len(r.Where) != 0 {
parser, err := GetWhereParserFn()(&Context{})
if err != nil {
return trace.Wrap(err)
}
_, err = parser.Parse(r.Where)
if err != nil {
return trace.BadParameter("could not parse 'where' rule: %q, error: %v", r.Where, err)
}
}
if len(r.Actions) != 0 {
parser, err := GetActionsParserFn()(&Context{})
if err != nil {
return trace.Wrap(err)
}
for i, action := range r.Actions {
_, err = parser.Parse(action)
if err != nil {
return trace.BadParameter("could not parse action %v %q, error: %v", i, action, err)
}
}
}
return nil
} | go | func (r *Rule) CheckAndSetDefaults() error {
if len(r.Resources) == 0 {
return trace.BadParameter("missing resources to match")
}
if len(r.Verbs) == 0 {
return trace.BadParameter("missing verbs")
}
if len(r.Where) != 0 {
parser, err := GetWhereParserFn()(&Context{})
if err != nil {
return trace.Wrap(err)
}
_, err = parser.Parse(r.Where)
if err != nil {
return trace.BadParameter("could not parse 'where' rule: %q, error: %v", r.Where, err)
}
}
if len(r.Actions) != 0 {
parser, err := GetActionsParserFn()(&Context{})
if err != nil {
return trace.Wrap(err)
}
for i, action := range r.Actions {
_, err = parser.Parse(action)
if err != nil {
return trace.BadParameter("could not parse action %v %q, error: %v", i, action, err)
}
}
}
return nil
} | [
"func",
"(",
"r",
"*",
"Rule",
")",
"CheckAndSetDefaults",
"(",
")",
"error",
"{",
"if",
"len",
"(",
"r",
".",
"Resources",
")",
"==",
"0",
"{",
"return",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"len",
"(",
"r",
".",
"Verbs",
")",
"==",
"0",
"{",
"return",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"len",
"(",
"r",
".",
"Where",
")",
"!=",
"0",
"{",
"parser",
",",
"err",
":=",
"GetWhereParserFn",
"(",
")",
"(",
"&",
"Context",
"{",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"_",
",",
"err",
"=",
"parser",
".",
"Parse",
"(",
"r",
".",
"Where",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"r",
".",
"Where",
",",
"err",
")",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"len",
"(",
"r",
".",
"Actions",
")",
"!=",
"0",
"{",
"parser",
",",
"err",
":=",
"GetActionsParserFn",
"(",
")",
"(",
"&",
"Context",
"{",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"for",
"i",
",",
"action",
":=",
"range",
"r",
".",
"Actions",
"{",
"_",
",",
"err",
"=",
"parser",
".",
"Parse",
"(",
"action",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"i",
",",
"action",
",",
"err",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // CheckAndSetDefaults checks and sets defaults for this rule | [
"CheckAndSetDefaults",
"checks",
"and",
"sets",
"defaults",
"for",
"this",
"rule"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L688-L718 | train |
gravitational/teleport | lib/services/role.go | score | func (r *Rule) score() int {
score := 0
// wilcard rules are less specific
if utils.SliceContainsStr(r.Resources, Wildcard) {
score -= 4
} else if len(r.Resources) == 1 {
// rules that match specific resource are more specific than
// fields that match several resources
score += 2
}
// rules that have wilcard verbs are less specific
if utils.SliceContainsStr(r.Verbs, Wildcard) {
score -= 2
}
// rules that supply 'where' or 'actions' are more specific
// having 'where' or 'actions' is more important than
// whether the rules are wildcard or not, so here we have +8 vs
// -4 and -2 score penalty for wildcards in resources and verbs
if len(r.Where) > 0 {
score += 8
}
// rules featuring actions are more specific
if len(r.Actions) > 0 {
score += 8
}
return score
} | go | func (r *Rule) score() int {
score := 0
// wilcard rules are less specific
if utils.SliceContainsStr(r.Resources, Wildcard) {
score -= 4
} else if len(r.Resources) == 1 {
// rules that match specific resource are more specific than
// fields that match several resources
score += 2
}
// rules that have wilcard verbs are less specific
if utils.SliceContainsStr(r.Verbs, Wildcard) {
score -= 2
}
// rules that supply 'where' or 'actions' are more specific
// having 'where' or 'actions' is more important than
// whether the rules are wildcard or not, so here we have +8 vs
// -4 and -2 score penalty for wildcards in resources and verbs
if len(r.Where) > 0 {
score += 8
}
// rules featuring actions are more specific
if len(r.Actions) > 0 {
score += 8
}
return score
} | [
"func",
"(",
"r",
"*",
"Rule",
")",
"score",
"(",
")",
"int",
"{",
"score",
":=",
"0",
"\n",
"// wilcard rules are less specific",
"if",
"utils",
".",
"SliceContainsStr",
"(",
"r",
".",
"Resources",
",",
"Wildcard",
")",
"{",
"score",
"-=",
"4",
"\n",
"}",
"else",
"if",
"len",
"(",
"r",
".",
"Resources",
")",
"==",
"1",
"{",
"// rules that match specific resource are more specific than",
"// fields that match several resources",
"score",
"+=",
"2",
"\n",
"}",
"\n",
"// rules that have wilcard verbs are less specific",
"if",
"utils",
".",
"SliceContainsStr",
"(",
"r",
".",
"Verbs",
",",
"Wildcard",
")",
"{",
"score",
"-=",
"2",
"\n",
"}",
"\n",
"// rules that supply 'where' or 'actions' are more specific",
"// having 'where' or 'actions' is more important than",
"// whether the rules are wildcard or not, so here we have +8 vs",
"// -4 and -2 score penalty for wildcards in resources and verbs",
"if",
"len",
"(",
"r",
".",
"Where",
")",
">",
"0",
"{",
"score",
"+=",
"8",
"\n",
"}",
"\n",
"// rules featuring actions are more specific",
"if",
"len",
"(",
"r",
".",
"Actions",
")",
">",
"0",
"{",
"score",
"+=",
"8",
"\n",
"}",
"\n",
"return",
"score",
"\n",
"}"
] | // score is a sorting score of the rule, the more the score, the more
// specific the rule is | [
"score",
"is",
"a",
"sorting",
"score",
"of",
"the",
"rule",
"the",
"more",
"the",
"score",
"the",
"more",
"specific",
"the",
"rule",
"is"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L722-L748 | train |
gravitational/teleport | lib/services/role.go | MatchesWhere | func (r *Rule) MatchesWhere(parser predicate.Parser) (bool, error) {
if r.Where == "" {
return true, nil
}
ifn, err := parser.Parse(r.Where)
if err != nil {
return false, trace.Wrap(err)
}
fn, ok := ifn.(predicate.BoolPredicate)
if !ok {
return false, trace.BadParameter("unsupported type: %T", ifn)
}
return fn(), nil
} | go | func (r *Rule) MatchesWhere(parser predicate.Parser) (bool, error) {
if r.Where == "" {
return true, nil
}
ifn, err := parser.Parse(r.Where)
if err != nil {
return false, trace.Wrap(err)
}
fn, ok := ifn.(predicate.BoolPredicate)
if !ok {
return false, trace.BadParameter("unsupported type: %T", ifn)
}
return fn(), nil
} | [
"func",
"(",
"r",
"*",
"Rule",
")",
"MatchesWhere",
"(",
"parser",
"predicate",
".",
"Parser",
")",
"(",
"bool",
",",
"error",
")",
"{",
"if",
"r",
".",
"Where",
"==",
"\"",
"\"",
"{",
"return",
"true",
",",
"nil",
"\n",
"}",
"\n",
"ifn",
",",
"err",
":=",
"parser",
".",
"Parse",
"(",
"r",
".",
"Where",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"false",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"fn",
",",
"ok",
":=",
"ifn",
".",
"(",
"predicate",
".",
"BoolPredicate",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"false",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"ifn",
")",
"\n",
"}",
"\n",
"return",
"fn",
"(",
")",
",",
"nil",
"\n",
"}"
] | // MatchesWhere returns true if Where rule matches
// Empty Where block always matches | [
"MatchesWhere",
"returns",
"true",
"if",
"Where",
"rule",
"matches",
"Empty",
"Where",
"block",
"always",
"matches"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L766-L779 | train |
gravitational/teleport | lib/services/role.go | ProcessActions | func (r *Rule) ProcessActions(parser predicate.Parser) error {
for _, action := range r.Actions {
ifn, err := parser.Parse(action)
if err != nil {
return trace.Wrap(err)
}
fn, ok := ifn.(predicate.BoolPredicate)
if !ok {
return trace.BadParameter("unsupported type: %T", ifn)
}
fn()
}
return nil
} | go | func (r *Rule) ProcessActions(parser predicate.Parser) error {
for _, action := range r.Actions {
ifn, err := parser.Parse(action)
if err != nil {
return trace.Wrap(err)
}
fn, ok := ifn.(predicate.BoolPredicate)
if !ok {
return trace.BadParameter("unsupported type: %T", ifn)
}
fn()
}
return nil
} | [
"func",
"(",
"r",
"*",
"Rule",
")",
"ProcessActions",
"(",
"parser",
"predicate",
".",
"Parser",
")",
"error",
"{",
"for",
"_",
",",
"action",
":=",
"range",
"r",
".",
"Actions",
"{",
"ifn",
",",
"err",
":=",
"parser",
".",
"Parse",
"(",
"action",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"fn",
",",
"ok",
":=",
"ifn",
".",
"(",
"predicate",
".",
"BoolPredicate",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"ifn",
")",
"\n",
"}",
"\n",
"fn",
"(",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // ProcessActions processes actions specified for this rule | [
"ProcessActions",
"processes",
"actions",
"specified",
"for",
"this",
"rule"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L782-L795 | train |
gravitational/teleport | lib/services/role.go | HasResource | func (r *Rule) HasResource(resource string) bool {
for _, r := range r.Resources {
if r == resource {
return true
}
}
return false
} | go | func (r *Rule) HasResource(resource string) bool {
for _, r := range r.Resources {
if r == resource {
return true
}
}
return false
} | [
"func",
"(",
"r",
"*",
"Rule",
")",
"HasResource",
"(",
"resource",
"string",
")",
"bool",
"{",
"for",
"_",
",",
"r",
":=",
"range",
"r",
".",
"Resources",
"{",
"if",
"r",
"==",
"resource",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] | // HasResource returns true if the rule has the specified resource. | [
"HasResource",
"returns",
"true",
"if",
"the",
"rule",
"has",
"the",
"specified",
"resource",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L798-L805 | train |
gravitational/teleport | lib/services/role.go | HasVerb | func (r *Rule) HasVerb(verb string) bool {
for _, v := range r.Verbs {
// readnosecrets can be satisfied by having readnosecrets or read
if verb == VerbReadNoSecrets {
if v == VerbReadNoSecrets || v == VerbRead {
return true
}
continue
}
if v == verb {
return true
}
}
return false
} | go | func (r *Rule) HasVerb(verb string) bool {
for _, v := range r.Verbs {
// readnosecrets can be satisfied by having readnosecrets or read
if verb == VerbReadNoSecrets {
if v == VerbReadNoSecrets || v == VerbRead {
return true
}
continue
}
if v == verb {
return true
}
}
return false
} | [
"func",
"(",
"r",
"*",
"Rule",
")",
"HasVerb",
"(",
"verb",
"string",
")",
"bool",
"{",
"for",
"_",
",",
"v",
":=",
"range",
"r",
".",
"Verbs",
"{",
"// readnosecrets can be satisfied by having readnosecrets or read",
"if",
"verb",
"==",
"VerbReadNoSecrets",
"{",
"if",
"v",
"==",
"VerbReadNoSecrets",
"||",
"v",
"==",
"VerbRead",
"{",
"return",
"true",
"\n",
"}",
"\n",
"continue",
"\n",
"}",
"\n",
"if",
"v",
"==",
"verb",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] | // HasVerb returns true if the rule has verb,
// this method also matches wildcard | [
"HasVerb",
"returns",
"true",
"if",
"the",
"rule",
"has",
"verb",
"this",
"method",
"also",
"matches",
"wildcard"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L809-L823 | train |
gravitational/teleport | lib/services/role.go | Equals | func (r *Rule) Equals(other Rule) bool {
if !utils.StringSlicesEqual(r.Resources, other.Resources) {
return false
}
if !utils.StringSlicesEqual(r.Verbs, other.Verbs) {
return false
}
if !utils.StringSlicesEqual(r.Actions, other.Actions) {
return false
}
if r.Where != other.Where {
return false
}
return true
} | go | func (r *Rule) Equals(other Rule) bool {
if !utils.StringSlicesEqual(r.Resources, other.Resources) {
return false
}
if !utils.StringSlicesEqual(r.Verbs, other.Verbs) {
return false
}
if !utils.StringSlicesEqual(r.Actions, other.Actions) {
return false
}
if r.Where != other.Where {
return false
}
return true
} | [
"func",
"(",
"r",
"*",
"Rule",
")",
"Equals",
"(",
"other",
"Rule",
")",
"bool",
"{",
"if",
"!",
"utils",
".",
"StringSlicesEqual",
"(",
"r",
".",
"Resources",
",",
"other",
".",
"Resources",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"if",
"!",
"utils",
".",
"StringSlicesEqual",
"(",
"r",
".",
"Verbs",
",",
"other",
".",
"Verbs",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"if",
"!",
"utils",
".",
"StringSlicesEqual",
"(",
"r",
".",
"Actions",
",",
"other",
".",
"Actions",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"if",
"r",
".",
"Where",
"!=",
"other",
".",
"Where",
"{",
"return",
"false",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}"
] | // Equals returns true if the rule equals to another | [
"Equals",
"returns",
"true",
"if",
"the",
"rule",
"equals",
"to",
"another"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L826-L840 | train |
gravitational/teleport | lib/services/role.go | Slice | func (set RuleSet) Slice() []Rule {
var out []Rule
for _, rules := range set {
out = append(out, rules...)
}
return out
} | go | func (set RuleSet) Slice() []Rule {
var out []Rule
for _, rules := range set {
out = append(out, rules...)
}
return out
} | [
"func",
"(",
"set",
"RuleSet",
")",
"Slice",
"(",
")",
"[",
"]",
"Rule",
"{",
"var",
"out",
"[",
"]",
"Rule",
"\n",
"for",
"_",
",",
"rules",
":=",
"range",
"set",
"{",
"out",
"=",
"append",
"(",
"out",
",",
"rules",
"...",
")",
"\n",
"}",
"\n",
"return",
"out",
"\n",
"}"
] | // Slice returns slice from a set | [
"Slice",
"returns",
"slice",
"from",
"a",
"set"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L893-L899 | train |
gravitational/teleport | lib/services/role.go | MakeRuleSet | func MakeRuleSet(rules []Rule) RuleSet {
set := make(RuleSet)
for _, rule := range rules {
for _, resource := range rule.Resources {
rules, ok := set[resource]
if !ok {
set[resource] = []Rule{rule}
} else {
rules = append(rules, rule)
set[resource] = rules
}
}
}
for resource := range set {
rules := set[resource]
// sort rules by most specific rule, the rule that has actions
// is more specific than the one that has no actions
sort.Slice(rules, func(i, j int) bool {
return rules[i].IsMoreSpecificThan(rules[j])
})
set[resource] = rules
}
return set
} | go | func MakeRuleSet(rules []Rule) RuleSet {
set := make(RuleSet)
for _, rule := range rules {
for _, resource := range rule.Resources {
rules, ok := set[resource]
if !ok {
set[resource] = []Rule{rule}
} else {
rules = append(rules, rule)
set[resource] = rules
}
}
}
for resource := range set {
rules := set[resource]
// sort rules by most specific rule, the rule that has actions
// is more specific than the one that has no actions
sort.Slice(rules, func(i, j int) bool {
return rules[i].IsMoreSpecificThan(rules[j])
})
set[resource] = rules
}
return set
} | [
"func",
"MakeRuleSet",
"(",
"rules",
"[",
"]",
"Rule",
")",
"RuleSet",
"{",
"set",
":=",
"make",
"(",
"RuleSet",
")",
"\n",
"for",
"_",
",",
"rule",
":=",
"range",
"rules",
"{",
"for",
"_",
",",
"resource",
":=",
"range",
"rule",
".",
"Resources",
"{",
"rules",
",",
"ok",
":=",
"set",
"[",
"resource",
"]",
"\n",
"if",
"!",
"ok",
"{",
"set",
"[",
"resource",
"]",
"=",
"[",
"]",
"Rule",
"{",
"rule",
"}",
"\n",
"}",
"else",
"{",
"rules",
"=",
"append",
"(",
"rules",
",",
"rule",
")",
"\n",
"set",
"[",
"resource",
"]",
"=",
"rules",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"for",
"resource",
":=",
"range",
"set",
"{",
"rules",
":=",
"set",
"[",
"resource",
"]",
"\n",
"// sort rules by most specific rule, the rule that has actions",
"// is more specific than the one that has no actions",
"sort",
".",
"Slice",
"(",
"rules",
",",
"func",
"(",
"i",
",",
"j",
"int",
")",
"bool",
"{",
"return",
"rules",
"[",
"i",
"]",
".",
"IsMoreSpecificThan",
"(",
"rules",
"[",
"j",
"]",
")",
"\n",
"}",
")",
"\n",
"set",
"[",
"resource",
"]",
"=",
"rules",
"\n",
"}",
"\n",
"return",
"set",
"\n",
"}"
] | // MakeRuleSet converts slice of rules to the set of rules | [
"MakeRuleSet",
"converts",
"slice",
"of",
"rules",
"to",
"the",
"set",
"of",
"rules"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L902-L925 | train |
gravitational/teleport | lib/services/role.go | CopyRulesSlice | func CopyRulesSlice(in []Rule) []Rule {
out := make([]Rule, len(in))
copy(out, in)
return out
} | go | func CopyRulesSlice(in []Rule) []Rule {
out := make([]Rule, len(in))
copy(out, in)
return out
} | [
"func",
"CopyRulesSlice",
"(",
"in",
"[",
"]",
"Rule",
")",
"[",
"]",
"Rule",
"{",
"out",
":=",
"make",
"(",
"[",
"]",
"Rule",
",",
"len",
"(",
"in",
")",
")",
"\n",
"copy",
"(",
"out",
",",
"in",
")",
"\n",
"return",
"out",
"\n",
"}"
] | // CopyRulesSlice copies input slice of Rules and returns the copy | [
"CopyRulesSlice",
"copies",
"input",
"slice",
"of",
"Rules",
"and",
"returns",
"the",
"copy"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L928-L932 | train |
gravitational/teleport | lib/services/role.go | RuleSlicesEqual | func RuleSlicesEqual(a, b []Rule) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if !a[i].Equals(b[i]) {
return false
}
}
return true
} | go | func RuleSlicesEqual(a, b []Rule) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if !a[i].Equals(b[i]) {
return false
}
}
return true
} | [
"func",
"RuleSlicesEqual",
"(",
"a",
",",
"b",
"[",
"]",
"Rule",
")",
"bool",
"{",
"if",
"len",
"(",
"a",
")",
"!=",
"len",
"(",
"b",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"for",
"i",
":=",
"range",
"a",
"{",
"if",
"!",
"a",
"[",
"i",
"]",
".",
"Equals",
"(",
"b",
"[",
"i",
"]",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}"
] | // RuleSlicesEqual returns true if two rule slices are equal | [
"RuleSlicesEqual",
"returns",
"true",
"if",
"two",
"rule",
"slices",
"are",
"equal"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L935-L945 | train |
gravitational/teleport | lib/services/role.go | Equals | func (r *RoleV2) Equals(other Role) bool {
return r.V3().Equals(other)
} | go | func (r *RoleV2) Equals(other Role) bool {
return r.V3().Equals(other)
} | [
"func",
"(",
"r",
"*",
"RoleV2",
")",
"Equals",
"(",
"other",
"Role",
")",
"bool",
"{",
"return",
"r",
".",
"V3",
"(",
")",
".",
"Equals",
"(",
"other",
")",
"\n",
"}"
] | // Equals test roles for equality. Roles are considered equal if all resources,
// logins, namespaces, labels, and options match. | [
"Equals",
"test",
"roles",
"for",
"equality",
".",
"Roles",
"are",
"considered",
"equal",
"if",
"all",
"resources",
"logins",
"namespaces",
"labels",
"and",
"options",
"match",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L993-L995 | train |
gravitational/teleport | lib/services/role.go | SetResource | func (r *RoleV2) SetResource(kind string, actions []string) {
if r.Spec.Resources == nil {
r.Spec.Resources = make(map[string][]string)
}
r.Spec.Resources[kind] = actions
} | go | func (r *RoleV2) SetResource(kind string, actions []string) {
if r.Spec.Resources == nil {
r.Spec.Resources = make(map[string][]string)
}
r.Spec.Resources[kind] = actions
} | [
"func",
"(",
"r",
"*",
"RoleV2",
")",
"SetResource",
"(",
"kind",
"string",
",",
"actions",
"[",
"]",
"string",
")",
"{",
"if",
"r",
".",
"Spec",
".",
"Resources",
"==",
"nil",
"{",
"r",
".",
"Spec",
".",
"Resources",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"[",
"]",
"string",
")",
"\n",
"}",
"\n",
"r",
".",
"Spec",
".",
"Resources",
"[",
"kind",
"]",
"=",
"actions",
"\n",
"}"
] | // SetResource sets resource rule | [
"SetResource",
"sets",
"resource",
"rule"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L998-L1003 | train |
gravitational/teleport | lib/services/role.go | RemoveResource | func (r *RoleV2) RemoveResource(kind string) {
delete(r.Spec.Resources, kind)
} | go | func (r *RoleV2) RemoveResource(kind string) {
delete(r.Spec.Resources, kind)
} | [
"func",
"(",
"r",
"*",
"RoleV2",
")",
"RemoveResource",
"(",
"kind",
"string",
")",
"{",
"delete",
"(",
"r",
".",
"Spec",
".",
"Resources",
",",
"kind",
")",
"\n",
"}"
] | // RemoveResource deletes resource entry | [
"RemoveResource",
"deletes",
"resource",
"entry"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1006-L1008 | train |
gravitational/teleport | lib/services/role.go | SetNodeLabels | func (r *RoleV2) SetNodeLabels(labels map[string]string) {
r.Spec.NodeLabels = labels
} | go | func (r *RoleV2) SetNodeLabels(labels map[string]string) {
r.Spec.NodeLabels = labels
} | [
"func",
"(",
"r",
"*",
"RoleV2",
")",
"SetNodeLabels",
"(",
"labels",
"map",
"[",
"string",
"]",
"string",
")",
"{",
"r",
".",
"Spec",
".",
"NodeLabels",
"=",
"labels",
"\n",
"}"
] | // SetNodeLabels sets node labels for role | [
"SetNodeLabels",
"sets",
"node",
"labels",
"for",
"role"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1016-L1018 | train |
gravitational/teleport | lib/services/role.go | SetMaxSessionTTL | func (r *RoleV2) SetMaxSessionTTL(duration time.Duration) {
r.Spec.MaxSessionTTL = Duration(duration)
} | go | func (r *RoleV2) SetMaxSessionTTL(duration time.Duration) {
r.Spec.MaxSessionTTL = Duration(duration)
} | [
"func",
"(",
"r",
"*",
"RoleV2",
")",
"SetMaxSessionTTL",
"(",
"duration",
"time",
".",
"Duration",
")",
"{",
"r",
".",
"Spec",
".",
"MaxSessionTTL",
"=",
"Duration",
"(",
"duration",
")",
"\n",
"}"
] | // SetMaxSessionTTL sets a maximum TTL for SSH or Web session | [
"SetMaxSessionTTL",
"sets",
"a",
"maximum",
"TTL",
"for",
"SSH",
"or",
"Web",
"session"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1021-L1023 | train |
gravitational/teleport | lib/services/role.go | FromSpec | func FromSpec(name string, spec RoleSpecV3) (RoleSet, error) {
role, err := NewRole(name, spec)
if err != nil {
return nil, trace.Wrap(err)
}
return NewRoleSet(role), nil
} | go | func FromSpec(name string, spec RoleSpecV3) (RoleSet, error) {
role, err := NewRole(name, spec)
if err != nil {
return nil, trace.Wrap(err)
}
return NewRoleSet(role), nil
} | [
"func",
"FromSpec",
"(",
"name",
"string",
",",
"spec",
"RoleSpecV3",
")",
"(",
"RoleSet",
",",
"error",
")",
"{",
"role",
",",
"err",
":=",
"NewRole",
"(",
"name",
",",
"spec",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"return",
"NewRoleSet",
"(",
"role",
")",
",",
"nil",
"\n",
"}"
] | // FromSpec returns new RoleSet created from spec | [
"FromSpec",
"returns",
"new",
"RoleSet",
"created",
"from",
"spec"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1271-L1278 | train |
gravitational/teleport | lib/services/role.go | NewRole | func NewRole(name string, spec RoleSpecV3) (Role, error) {
role := RoleV3{
Kind: KindRole,
Version: V3,
Metadata: Metadata{
Name: name,
Namespace: defaults.Namespace,
},
Spec: spec,
}
if err := role.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return &role, nil
} | go | func NewRole(name string, spec RoleSpecV3) (Role, error) {
role := RoleV3{
Kind: KindRole,
Version: V3,
Metadata: Metadata{
Name: name,
Namespace: defaults.Namespace,
},
Spec: spec,
}
if err := role.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return &role, nil
} | [
"func",
"NewRole",
"(",
"name",
"string",
",",
"spec",
"RoleSpecV3",
")",
"(",
"Role",
",",
"error",
")",
"{",
"role",
":=",
"RoleV3",
"{",
"Kind",
":",
"KindRole",
",",
"Version",
":",
"V3",
",",
"Metadata",
":",
"Metadata",
"{",
"Name",
":",
"name",
",",
"Namespace",
":",
"defaults",
".",
"Namespace",
",",
"}",
",",
"Spec",
":",
"spec",
",",
"}",
"\n",
"if",
"err",
":=",
"role",
".",
"CheckAndSetDefaults",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"return",
"&",
"role",
",",
"nil",
"\n",
"}"
] | // NewRole constructs new standard role | [
"NewRole",
"constructs",
"new",
"standard",
"role"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1297-L1312 | train |
gravitational/teleport | lib/services/role.go | FetchRoles | func FetchRoles(roleNames []string, access RoleGetter, traits map[string][]string) (RoleSet, error) {
var roles []Role
for _, roleName := range roleNames {
role, err := access.GetRole(roleName)
if err != nil {
return nil, trace.Wrap(err)
}
roles = append(roles, role.ApplyTraits(traits))
}
return NewRoleSet(roles...), nil
} | go | func FetchRoles(roleNames []string, access RoleGetter, traits map[string][]string) (RoleSet, error) {
var roles []Role
for _, roleName := range roleNames {
role, err := access.GetRole(roleName)
if err != nil {
return nil, trace.Wrap(err)
}
roles = append(roles, role.ApplyTraits(traits))
}
return NewRoleSet(roles...), nil
} | [
"func",
"FetchRoles",
"(",
"roleNames",
"[",
"]",
"string",
",",
"access",
"RoleGetter",
",",
"traits",
"map",
"[",
"string",
"]",
"[",
"]",
"string",
")",
"(",
"RoleSet",
",",
"error",
")",
"{",
"var",
"roles",
"[",
"]",
"Role",
"\n\n",
"for",
"_",
",",
"roleName",
":=",
"range",
"roleNames",
"{",
"role",
",",
"err",
":=",
"access",
".",
"GetRole",
"(",
"roleName",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"roles",
"=",
"append",
"(",
"roles",
",",
"role",
".",
"ApplyTraits",
"(",
"traits",
")",
")",
"\n",
"}",
"\n\n",
"return",
"NewRoleSet",
"(",
"roles",
"...",
")",
",",
"nil",
"\n",
"}"
] | // FetchRoles fetches roles by their names, applies the traits to role
// variables, and returns the RoleSet. | [
"FetchRoles",
"fetches",
"roles",
"by",
"their",
"names",
"applies",
"the",
"traits",
"to",
"role",
"variables",
"and",
"returns",
"the",
"RoleSet",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1322-L1334 | train |
gravitational/teleport | lib/services/role.go | NewRoleSet | func NewRoleSet(roles ...Role) RoleSet {
// unauthenticated Nop role should not have any privileges
// by default, otherwise it is too permissive
if len(roles) == 1 && roles[0].GetName() == string(teleport.RoleNop) {
return roles
}
return append(roles, NewImplicitRole())
} | go | func NewRoleSet(roles ...Role) RoleSet {
// unauthenticated Nop role should not have any privileges
// by default, otherwise it is too permissive
if len(roles) == 1 && roles[0].GetName() == string(teleport.RoleNop) {
return roles
}
return append(roles, NewImplicitRole())
} | [
"func",
"NewRoleSet",
"(",
"roles",
"...",
"Role",
")",
"RoleSet",
"{",
"// unauthenticated Nop role should not have any privileges",
"// by default, otherwise it is too permissive",
"if",
"len",
"(",
"roles",
")",
"==",
"1",
"&&",
"roles",
"[",
"0",
"]",
".",
"GetName",
"(",
")",
"==",
"string",
"(",
"teleport",
".",
"RoleNop",
")",
"{",
"return",
"roles",
"\n",
"}",
"\n",
"return",
"append",
"(",
"roles",
",",
"NewImplicitRole",
"(",
")",
")",
"\n",
"}"
] | // NewRoleSet returns new RoleSet based on the roles | [
"NewRoleSet",
"returns",
"new",
"RoleSet",
"based",
"on",
"the",
"roles"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1337-L1344 | train |
gravitational/teleport | lib/services/role.go | MatchNamespace | func MatchNamespace(selectors []string, namespace string) (bool, string) {
for _, n := range selectors {
if n == namespace || n == Wildcard {
return true, "matched"
}
}
return false, fmt.Sprintf("no match, role selectors %v, server namespace: %v", selectors, namespace)
} | go | func MatchNamespace(selectors []string, namespace string) (bool, string) {
for _, n := range selectors {
if n == namespace || n == Wildcard {
return true, "matched"
}
}
return false, fmt.Sprintf("no match, role selectors %v, server namespace: %v", selectors, namespace)
} | [
"func",
"MatchNamespace",
"(",
"selectors",
"[",
"]",
"string",
",",
"namespace",
"string",
")",
"(",
"bool",
",",
"string",
")",
"{",
"for",
"_",
",",
"n",
":=",
"range",
"selectors",
"{",
"if",
"n",
"==",
"namespace",
"||",
"n",
"==",
"Wildcard",
"{",
"return",
"true",
",",
"\"",
"\"",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
",",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"selectors",
",",
"namespace",
")",
"\n",
"}"
] | // MatchNamespace returns true if given list of namespace matches
// target namespace, wildcard matches everything. | [
"MatchNamespace",
"returns",
"true",
"if",
"given",
"list",
"of",
"namespace",
"matches",
"target",
"namespace",
"wildcard",
"matches",
"everything",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1351-L1358 | train |
gravitational/teleport | lib/services/role.go | MatchLogin | func MatchLogin(selectors []string, login string) (bool, string) {
for _, l := range selectors {
if l == login {
return true, "matched"
}
}
return false, fmt.Sprintf("no match, role selectors %v, login: %v", selectors, login)
} | go | func MatchLogin(selectors []string, login string) (bool, string) {
for _, l := range selectors {
if l == login {
return true, "matched"
}
}
return false, fmt.Sprintf("no match, role selectors %v, login: %v", selectors, login)
} | [
"func",
"MatchLogin",
"(",
"selectors",
"[",
"]",
"string",
",",
"login",
"string",
")",
"(",
"bool",
",",
"string",
")",
"{",
"for",
"_",
",",
"l",
":=",
"range",
"selectors",
"{",
"if",
"l",
"==",
"login",
"{",
"return",
"true",
",",
"\"",
"\"",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
",",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"selectors",
",",
"login",
")",
"\n",
"}"
] | // MatchLogin returns true if attempted login matches any of the logins. | [
"MatchLogin",
"returns",
"true",
"if",
"attempted",
"login",
"matches",
"any",
"of",
"the",
"logins",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1361-L1368 | train |
gravitational/teleport | lib/services/role.go | MatchLabels | func MatchLabels(selector Labels, target map[string]string) (bool, string, error) {
// Empty selector matches nothing.
if len(selector) == 0 {
return false, "no match, empty selector", nil
}
// *: * matches everything even empty target set.
selectorValues := selector[Wildcard]
if len(selectorValues) == 1 && selectorValues[0] == Wildcard {
return true, "matched", nil
}
// Perform full match.
for key, selectorValues := range selector {
targetVal, hasKey := target[key]
if !hasKey {
return false, fmt.Sprintf("no key match: '%v'", key), nil
}
if !utils.SliceContainsStr(selectorValues, Wildcard) {
result, err := utils.SliceMatchesRegex(targetVal, selectorValues)
if err != nil {
return false, "", trace.Wrap(err)
} else if !result {
return false, fmt.Sprintf("no value match: got '%v' want: '%v'", targetVal, selectorValues), nil
}
}
}
return true, "matched", nil
} | go | func MatchLabels(selector Labels, target map[string]string) (bool, string, error) {
// Empty selector matches nothing.
if len(selector) == 0 {
return false, "no match, empty selector", nil
}
// *: * matches everything even empty target set.
selectorValues := selector[Wildcard]
if len(selectorValues) == 1 && selectorValues[0] == Wildcard {
return true, "matched", nil
}
// Perform full match.
for key, selectorValues := range selector {
targetVal, hasKey := target[key]
if !hasKey {
return false, fmt.Sprintf("no key match: '%v'", key), nil
}
if !utils.SliceContainsStr(selectorValues, Wildcard) {
result, err := utils.SliceMatchesRegex(targetVal, selectorValues)
if err != nil {
return false, "", trace.Wrap(err)
} else if !result {
return false, fmt.Sprintf("no value match: got '%v' want: '%v'", targetVal, selectorValues), nil
}
}
}
return true, "matched", nil
} | [
"func",
"MatchLabels",
"(",
"selector",
"Labels",
",",
"target",
"map",
"[",
"string",
"]",
"string",
")",
"(",
"bool",
",",
"string",
",",
"error",
")",
"{",
"// Empty selector matches nothing.",
"if",
"len",
"(",
"selector",
")",
"==",
"0",
"{",
"return",
"false",
",",
"\"",
"\"",
",",
"nil",
"\n",
"}",
"\n\n",
"// *: * matches everything even empty target set.",
"selectorValues",
":=",
"selector",
"[",
"Wildcard",
"]",
"\n",
"if",
"len",
"(",
"selectorValues",
")",
"==",
"1",
"&&",
"selectorValues",
"[",
"0",
"]",
"==",
"Wildcard",
"{",
"return",
"true",
",",
"\"",
"\"",
",",
"nil",
"\n",
"}",
"\n\n",
"// Perform full match.",
"for",
"key",
",",
"selectorValues",
":=",
"range",
"selector",
"{",
"targetVal",
",",
"hasKey",
":=",
"target",
"[",
"key",
"]",
"\n\n",
"if",
"!",
"hasKey",
"{",
"return",
"false",
",",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"key",
")",
",",
"nil",
"\n",
"}",
"\n\n",
"if",
"!",
"utils",
".",
"SliceContainsStr",
"(",
"selectorValues",
",",
"Wildcard",
")",
"{",
"result",
",",
"err",
":=",
"utils",
".",
"SliceMatchesRegex",
"(",
"targetVal",
",",
"selectorValues",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"false",
",",
"\"",
"\"",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"else",
"if",
"!",
"result",
"{",
"return",
"false",
",",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"targetVal",
",",
"selectorValues",
")",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"true",
",",
"\"",
"\"",
",",
"nil",
"\n",
"}"
] | // MatchLabels matches selector against target. Empty selector matches
// nothing, wildcard matches everything. | [
"MatchLabels",
"matches",
"selector",
"against",
"target",
".",
"Empty",
"selector",
"matches",
"nothing",
"wildcard",
"matches",
"everything",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1372-L1403 | train |
gravitational/teleport | lib/services/role.go | RoleNames | func (set RoleSet) RoleNames() []string {
out := make([]string, len(set))
for i, r := range set {
out[i] = r.GetName()
}
return out
} | go | func (set RoleSet) RoleNames() []string {
out := make([]string, len(set))
for i, r := range set {
out[i] = r.GetName()
}
return out
} | [
"func",
"(",
"set",
"RoleSet",
")",
"RoleNames",
"(",
")",
"[",
"]",
"string",
"{",
"out",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"set",
")",
")",
"\n",
"for",
"i",
",",
"r",
":=",
"range",
"set",
"{",
"out",
"[",
"i",
"]",
"=",
"r",
".",
"GetName",
"(",
")",
"\n",
"}",
"\n",
"return",
"out",
"\n",
"}"
] | // RoleNames returns a slice with role names | [
"RoleNames",
"returns",
"a",
"slice",
"with",
"role",
"names"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1406-L1412 | train |
gravitational/teleport | lib/services/role.go | HasRole | func (set RoleSet) HasRole(role string) bool {
for _, r := range set {
if r.GetName() == role {
return true
}
}
return false
} | go | func (set RoleSet) HasRole(role string) bool {
for _, r := range set {
if r.GetName() == role {
return true
}
}
return false
} | [
"func",
"(",
"set",
"RoleSet",
")",
"HasRole",
"(",
"role",
"string",
")",
"bool",
"{",
"for",
"_",
",",
"r",
":=",
"range",
"set",
"{",
"if",
"r",
".",
"GetName",
"(",
")",
"==",
"role",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] | // HasRole checks if the role set has the role | [
"HasRole",
"checks",
"if",
"the",
"role",
"set",
"has",
"the",
"role"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1415-L1422 | train |
gravitational/teleport | lib/services/role.go | AdjustSessionTTL | func (set RoleSet) AdjustSessionTTL(ttl time.Duration) time.Duration {
for _, role := range set {
maxSessionTTL := role.GetOptions().MaxSessionTTL.Value()
if maxSessionTTL != 0 && ttl > maxSessionTTL {
ttl = maxSessionTTL
}
}
return ttl
} | go | func (set RoleSet) AdjustSessionTTL(ttl time.Duration) time.Duration {
for _, role := range set {
maxSessionTTL := role.GetOptions().MaxSessionTTL.Value()
if maxSessionTTL != 0 && ttl > maxSessionTTL {
ttl = maxSessionTTL
}
}
return ttl
} | [
"func",
"(",
"set",
"RoleSet",
")",
"AdjustSessionTTL",
"(",
"ttl",
"time",
".",
"Duration",
")",
"time",
".",
"Duration",
"{",
"for",
"_",
",",
"role",
":=",
"range",
"set",
"{",
"maxSessionTTL",
":=",
"role",
".",
"GetOptions",
"(",
")",
".",
"MaxSessionTTL",
".",
"Value",
"(",
")",
"\n",
"if",
"maxSessionTTL",
"!=",
"0",
"&&",
"ttl",
">",
"maxSessionTTL",
"{",
"ttl",
"=",
"maxSessionTTL",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"ttl",
"\n",
"}"
] | // AdjustSessionTTL will reduce the requested ttl to lowest max allowed TTL
// for this role set, otherwise it returns ttl unchanged | [
"AdjustSessionTTL",
"will",
"reduce",
"the",
"requested",
"ttl",
"to",
"lowest",
"max",
"allowed",
"TTL",
"for",
"this",
"role",
"set",
"otherwise",
"it",
"returns",
"ttl",
"unchanged"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1426-L1434 | train |
gravitational/teleport | lib/services/role.go | AdjustClientIdleTimeout | func (set RoleSet) AdjustClientIdleTimeout(timeout time.Duration) time.Duration {
if timeout < 0 {
timeout = 0
}
for _, role := range set {
roleTimeout := role.GetOptions().ClientIdleTimeout
// 0 means not set, so it can't be most restrictive, disregard it too
if roleTimeout.Duration() <= 0 {
continue
}
switch {
// in case if timeout is 0, means that incoming value
// does not restrict the idle timeout, pick any other value
// set by the role
case timeout == 0:
timeout = roleTimeout.Duration()
case roleTimeout.Duration() < timeout:
timeout = roleTimeout.Duration()
}
}
return timeout
} | go | func (set RoleSet) AdjustClientIdleTimeout(timeout time.Duration) time.Duration {
if timeout < 0 {
timeout = 0
}
for _, role := range set {
roleTimeout := role.GetOptions().ClientIdleTimeout
// 0 means not set, so it can't be most restrictive, disregard it too
if roleTimeout.Duration() <= 0 {
continue
}
switch {
// in case if timeout is 0, means that incoming value
// does not restrict the idle timeout, pick any other value
// set by the role
case timeout == 0:
timeout = roleTimeout.Duration()
case roleTimeout.Duration() < timeout:
timeout = roleTimeout.Duration()
}
}
return timeout
} | [
"func",
"(",
"set",
"RoleSet",
")",
"AdjustClientIdleTimeout",
"(",
"timeout",
"time",
".",
"Duration",
")",
"time",
".",
"Duration",
"{",
"if",
"timeout",
"<",
"0",
"{",
"timeout",
"=",
"0",
"\n",
"}",
"\n",
"for",
"_",
",",
"role",
":=",
"range",
"set",
"{",
"roleTimeout",
":=",
"role",
".",
"GetOptions",
"(",
")",
".",
"ClientIdleTimeout",
"\n",
"// 0 means not set, so it can't be most restrictive, disregard it too",
"if",
"roleTimeout",
".",
"Duration",
"(",
")",
"<=",
"0",
"{",
"continue",
"\n",
"}",
"\n",
"switch",
"{",
"// in case if timeout is 0, means that incoming value",
"// does not restrict the idle timeout, pick any other value",
"// set by the role",
"case",
"timeout",
"==",
"0",
":",
"timeout",
"=",
"roleTimeout",
".",
"Duration",
"(",
")",
"\n",
"case",
"roleTimeout",
".",
"Duration",
"(",
")",
"<",
"timeout",
":",
"timeout",
"=",
"roleTimeout",
".",
"Duration",
"(",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"timeout",
"\n",
"}"
] | // AdjustClientIdleTimeout adjusts requested idle timeout
// to the lowest max allowed timeout, the most restrictive
// option will be picked, negative values will be assumed as 0 | [
"AdjustClientIdleTimeout",
"adjusts",
"requested",
"idle",
"timeout",
"to",
"the",
"lowest",
"max",
"allowed",
"timeout",
"the",
"most",
"restrictive",
"option",
"will",
"be",
"picked",
"negative",
"values",
"will",
"be",
"assumed",
"as",
"0"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1439-L1460 | train |
gravitational/teleport | lib/services/role.go | AdjustDisconnectExpiredCert | func (set RoleSet) AdjustDisconnectExpiredCert(disconnect bool) bool {
for _, role := range set {
if role.GetOptions().DisconnectExpiredCert.Value() {
disconnect = true
}
}
return disconnect
} | go | func (set RoleSet) AdjustDisconnectExpiredCert(disconnect bool) bool {
for _, role := range set {
if role.GetOptions().DisconnectExpiredCert.Value() {
disconnect = true
}
}
return disconnect
} | [
"func",
"(",
"set",
"RoleSet",
")",
"AdjustDisconnectExpiredCert",
"(",
"disconnect",
"bool",
")",
"bool",
"{",
"for",
"_",
",",
"role",
":=",
"range",
"set",
"{",
"if",
"role",
".",
"GetOptions",
"(",
")",
".",
"DisconnectExpiredCert",
".",
"Value",
"(",
")",
"{",
"disconnect",
"=",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"disconnect",
"\n",
"}"
] | // AdjustDisconnectExpiredCert adjusts the value based on the role set
// the most restrictive option will be picked | [
"AdjustDisconnectExpiredCert",
"adjusts",
"the",
"value",
"based",
"on",
"the",
"role",
"set",
"the",
"most",
"restrictive",
"option",
"will",
"be",
"picked"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1464-L1471 | train |
gravitational/teleport | lib/services/role.go | CheckKubeGroups | func (set RoleSet) CheckKubeGroups(ttl time.Duration) ([]string, error) {
groups := make(map[string]bool)
var matchedTTL bool
for _, role := range set {
maxSessionTTL := role.GetOptions().MaxSessionTTL.Value()
if ttl <= maxSessionTTL && maxSessionTTL != 0 {
matchedTTL = true
for _, group := range role.GetKubeGroups(Allow) {
groups[group] = true
}
}
}
if !matchedTTL {
return nil, trace.AccessDenied("this user cannot request kubernetes access for %v", ttl)
}
if len(groups) == 0 {
return nil, trace.AccessDenied("this user cannot request kubernetes access, has no assigned groups")
}
out := make([]string, 0, len(groups))
for group := range groups {
out = append(out, group)
}
return out, nil
} | go | func (set RoleSet) CheckKubeGroups(ttl time.Duration) ([]string, error) {
groups := make(map[string]bool)
var matchedTTL bool
for _, role := range set {
maxSessionTTL := role.GetOptions().MaxSessionTTL.Value()
if ttl <= maxSessionTTL && maxSessionTTL != 0 {
matchedTTL = true
for _, group := range role.GetKubeGroups(Allow) {
groups[group] = true
}
}
}
if !matchedTTL {
return nil, trace.AccessDenied("this user cannot request kubernetes access for %v", ttl)
}
if len(groups) == 0 {
return nil, trace.AccessDenied("this user cannot request kubernetes access, has no assigned groups")
}
out := make([]string, 0, len(groups))
for group := range groups {
out = append(out, group)
}
return out, nil
} | [
"func",
"(",
"set",
"RoleSet",
")",
"CheckKubeGroups",
"(",
"ttl",
"time",
".",
"Duration",
")",
"(",
"[",
"]",
"string",
",",
"error",
")",
"{",
"groups",
":=",
"make",
"(",
"map",
"[",
"string",
"]",
"bool",
")",
"\n",
"var",
"matchedTTL",
"bool",
"\n",
"for",
"_",
",",
"role",
":=",
"range",
"set",
"{",
"maxSessionTTL",
":=",
"role",
".",
"GetOptions",
"(",
")",
".",
"MaxSessionTTL",
".",
"Value",
"(",
")",
"\n",
"if",
"ttl",
"<=",
"maxSessionTTL",
"&&",
"maxSessionTTL",
"!=",
"0",
"{",
"matchedTTL",
"=",
"true",
"\n",
"for",
"_",
",",
"group",
":=",
"range",
"role",
".",
"GetKubeGroups",
"(",
"Allow",
")",
"{",
"groups",
"[",
"group",
"]",
"=",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"!",
"matchedTTL",
"{",
"return",
"nil",
",",
"trace",
".",
"AccessDenied",
"(",
"\"",
"\"",
",",
"ttl",
")",
"\n",
"}",
"\n",
"if",
"len",
"(",
"groups",
")",
"==",
"0",
"{",
"return",
"nil",
",",
"trace",
".",
"AccessDenied",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"out",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"0",
",",
"len",
"(",
"groups",
")",
")",
"\n",
"for",
"group",
":=",
"range",
"groups",
"{",
"out",
"=",
"append",
"(",
"out",
",",
"group",
")",
"\n",
"}",
"\n",
"return",
"out",
",",
"nil",
"\n",
"}"
] | // CheckKubeGroups check if role can login into kubernetes
// and returns a combined list of allowed groups | [
"CheckKubeGroups",
"check",
"if",
"role",
"can",
"login",
"into",
"kubernetes",
"and",
"returns",
"a",
"combined",
"list",
"of",
"allowed",
"groups"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1475-L1498 | train |
gravitational/teleport | lib/services/role.go | CheckLoginDuration | func (set RoleSet) CheckLoginDuration(ttl time.Duration) ([]string, error) {
logins := make(map[string]bool)
var matchedTTL bool
for _, role := range set {
maxSessionTTL := role.GetOptions().MaxSessionTTL.Value()
if ttl <= maxSessionTTL && maxSessionTTL != 0 {
matchedTTL = true
for _, login := range role.GetLogins(Allow) {
logins[login] = true
}
}
}
if !matchedTTL {
return nil, trace.AccessDenied("this user cannot request a certificate for %v", ttl)
}
if len(logins) == 0 {
return nil, trace.AccessDenied("this user cannot create SSH sessions, has no allowed logins")
}
out := make([]string, 0, len(logins))
for login := range logins {
out = append(out, login)
}
return out, nil
} | go | func (set RoleSet) CheckLoginDuration(ttl time.Duration) ([]string, error) {
logins := make(map[string]bool)
var matchedTTL bool
for _, role := range set {
maxSessionTTL := role.GetOptions().MaxSessionTTL.Value()
if ttl <= maxSessionTTL && maxSessionTTL != 0 {
matchedTTL = true
for _, login := range role.GetLogins(Allow) {
logins[login] = true
}
}
}
if !matchedTTL {
return nil, trace.AccessDenied("this user cannot request a certificate for %v", ttl)
}
if len(logins) == 0 {
return nil, trace.AccessDenied("this user cannot create SSH sessions, has no allowed logins")
}
out := make([]string, 0, len(logins))
for login := range logins {
out = append(out, login)
}
return out, nil
} | [
"func",
"(",
"set",
"RoleSet",
")",
"CheckLoginDuration",
"(",
"ttl",
"time",
".",
"Duration",
")",
"(",
"[",
"]",
"string",
",",
"error",
")",
"{",
"logins",
":=",
"make",
"(",
"map",
"[",
"string",
"]",
"bool",
")",
"\n",
"var",
"matchedTTL",
"bool",
"\n",
"for",
"_",
",",
"role",
":=",
"range",
"set",
"{",
"maxSessionTTL",
":=",
"role",
".",
"GetOptions",
"(",
")",
".",
"MaxSessionTTL",
".",
"Value",
"(",
")",
"\n",
"if",
"ttl",
"<=",
"maxSessionTTL",
"&&",
"maxSessionTTL",
"!=",
"0",
"{",
"matchedTTL",
"=",
"true",
"\n\n",
"for",
"_",
",",
"login",
":=",
"range",
"role",
".",
"GetLogins",
"(",
"Allow",
")",
"{",
"logins",
"[",
"login",
"]",
"=",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"!",
"matchedTTL",
"{",
"return",
"nil",
",",
"trace",
".",
"AccessDenied",
"(",
"\"",
"\"",
",",
"ttl",
")",
"\n",
"}",
"\n",
"if",
"len",
"(",
"logins",
")",
"==",
"0",
"{",
"return",
"nil",
",",
"trace",
".",
"AccessDenied",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"out",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"0",
",",
"len",
"(",
"logins",
")",
")",
"\n",
"for",
"login",
":=",
"range",
"logins",
"{",
"out",
"=",
"append",
"(",
"out",
",",
"login",
")",
"\n",
"}",
"\n",
"return",
"out",
",",
"nil",
"\n",
"}"
] | // CheckLoginDuration checks if role set can login up to given duration and
// returns a combined list of allowed logins. | [
"CheckLoginDuration",
"checks",
"if",
"role",
"set",
"can",
"login",
"up",
"to",
"given",
"duration",
"and",
"returns",
"a",
"combined",
"list",
"of",
"allowed",
"logins",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1502-L1526 | train |
gravitational/teleport | lib/services/role.go | CanForwardAgents | func (set RoleSet) CanForwardAgents() bool {
for _, role := range set {
if role.GetOptions().ForwardAgent.Value() {
return true
}
}
return false
} | go | func (set RoleSet) CanForwardAgents() bool {
for _, role := range set {
if role.GetOptions().ForwardAgent.Value() {
return true
}
}
return false
} | [
"func",
"(",
"set",
"RoleSet",
")",
"CanForwardAgents",
"(",
")",
"bool",
"{",
"for",
"_",
",",
"role",
":=",
"range",
"set",
"{",
"if",
"role",
".",
"GetOptions",
"(",
")",
".",
"ForwardAgent",
".",
"Value",
"(",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] | // CanForwardAgents returns true if role set allows forwarding agents. | [
"CanForwardAgents",
"returns",
"true",
"if",
"role",
"set",
"allows",
"forwarding",
"agents",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1586-L1593 | train |
gravitational/teleport | lib/services/role.go | CanPortForward | func (set RoleSet) CanPortForward() bool {
for _, role := range set {
if BoolDefaultTrue(role.GetOptions().PortForwarding) {
return true
}
}
return false
} | go | func (set RoleSet) CanPortForward() bool {
for _, role := range set {
if BoolDefaultTrue(role.GetOptions().PortForwarding) {
return true
}
}
return false
} | [
"func",
"(",
"set",
"RoleSet",
")",
"CanPortForward",
"(",
")",
"bool",
"{",
"for",
"_",
",",
"role",
":=",
"range",
"set",
"{",
"if",
"BoolDefaultTrue",
"(",
"role",
".",
"GetOptions",
"(",
")",
".",
"PortForwarding",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] | // CanPortForward returns true if a role in the RoleSet allows port forwarding. | [
"CanPortForward",
"returns",
"true",
"if",
"a",
"role",
"in",
"the",
"RoleSet",
"allows",
"port",
"forwarding",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1596-L1603 | train |
gravitational/teleport | lib/services/role.go | CertificateFormat | func (set RoleSet) CertificateFormat() string {
var formats []string
for _, role := range set {
// get the certificate format for each individual role. if a role does not
// have a certificate format (like implicit roles) skip over it
certificateFormat := role.GetOptions().CertificateFormat
if certificateFormat == "" {
continue
}
formats = append(formats, certificateFormat)
}
// if no formats were found, return standard
if len(formats) == 0 {
return teleport.CertificateFormatStandard
}
// sort the slice so the most permissive is the first element
sort.Slice(formats, func(i, j int) bool {
return certificatePriority(formats[i]) < certificatePriority(formats[j])
})
return formats[0]
} | go | func (set RoleSet) CertificateFormat() string {
var formats []string
for _, role := range set {
// get the certificate format for each individual role. if a role does not
// have a certificate format (like implicit roles) skip over it
certificateFormat := role.GetOptions().CertificateFormat
if certificateFormat == "" {
continue
}
formats = append(formats, certificateFormat)
}
// if no formats were found, return standard
if len(formats) == 0 {
return teleport.CertificateFormatStandard
}
// sort the slice so the most permissive is the first element
sort.Slice(formats, func(i, j int) bool {
return certificatePriority(formats[i]) < certificatePriority(formats[j])
})
return formats[0]
} | [
"func",
"(",
"set",
"RoleSet",
")",
"CertificateFormat",
"(",
")",
"string",
"{",
"var",
"formats",
"[",
"]",
"string",
"\n\n",
"for",
"_",
",",
"role",
":=",
"range",
"set",
"{",
"// get the certificate format for each individual role. if a role does not",
"// have a certificate format (like implicit roles) skip over it",
"certificateFormat",
":=",
"role",
".",
"GetOptions",
"(",
")",
".",
"CertificateFormat",
"\n",
"if",
"certificateFormat",
"==",
"\"",
"\"",
"{",
"continue",
"\n",
"}",
"\n\n",
"formats",
"=",
"append",
"(",
"formats",
",",
"certificateFormat",
")",
"\n",
"}",
"\n\n",
"// if no formats were found, return standard",
"if",
"len",
"(",
"formats",
")",
"==",
"0",
"{",
"return",
"teleport",
".",
"CertificateFormatStandard",
"\n",
"}",
"\n\n",
"// sort the slice so the most permissive is the first element",
"sort",
".",
"Slice",
"(",
"formats",
",",
"func",
"(",
"i",
",",
"j",
"int",
")",
"bool",
"{",
"return",
"certificatePriority",
"(",
"formats",
"[",
"i",
"]",
")",
"<",
"certificatePriority",
"(",
"formats",
"[",
"j",
"]",
")",
"\n",
"}",
")",
"\n\n",
"return",
"formats",
"[",
"0",
"]",
"\n",
"}"
] | // CertificateFormat returns the most permissive certificate format in a
// RoleSet. | [
"CertificateFormat",
"returns",
"the",
"most",
"permissive",
"certificate",
"format",
"in",
"a",
"RoleSet",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1607-L1632 | train |
gravitational/teleport | lib/services/role.go | CheckAgentForward | func (set RoleSet) CheckAgentForward(login string) error {
// check if we have permission to login and forward agent. we don't check
// for deny rules because if you can't forward an agent if you can't login
// in the first place.
for _, role := range set {
for _, l := range role.GetLogins(Allow) {
if role.GetOptions().ForwardAgent.Value() && l == login {
return nil
}
}
}
return trace.AccessDenied("%v can not forward agent for %v", set, login)
} | go | func (set RoleSet) CheckAgentForward(login string) error {
// check if we have permission to login and forward agent. we don't check
// for deny rules because if you can't forward an agent if you can't login
// in the first place.
for _, role := range set {
for _, l := range role.GetLogins(Allow) {
if role.GetOptions().ForwardAgent.Value() && l == login {
return nil
}
}
}
return trace.AccessDenied("%v can not forward agent for %v", set, login)
} | [
"func",
"(",
"set",
"RoleSet",
")",
"CheckAgentForward",
"(",
"login",
"string",
")",
"error",
"{",
"// check if we have permission to login and forward agent. we don't check",
"// for deny rules because if you can't forward an agent if you can't login",
"// in the first place.",
"for",
"_",
",",
"role",
":=",
"range",
"set",
"{",
"for",
"_",
",",
"l",
":=",
"range",
"role",
".",
"GetLogins",
"(",
"Allow",
")",
"{",
"if",
"role",
".",
"GetOptions",
"(",
")",
".",
"ForwardAgent",
".",
"Value",
"(",
")",
"&&",
"l",
"==",
"login",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"trace",
".",
"AccessDenied",
"(",
"\"",
"\"",
",",
"set",
",",
"login",
")",
"\n",
"}"
] | // CheckAgentForward checks if the role can request to forward the SSH agent
// for this user. | [
"CheckAgentForward",
"checks",
"if",
"the",
"role",
"can",
"request",
"to",
"forward",
"the",
"SSH",
"agent",
"for",
"this",
"user",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1649-L1661 | train |
gravitational/teleport | lib/services/role.go | Clone | func (l Labels) Clone() Labels {
if l == nil {
return nil
}
out := make(Labels, len(l))
for key, vals := range l {
cvals := make([]string, len(vals))
copy(cvals, vals)
out[key] = cvals
}
return out
} | go | func (l Labels) Clone() Labels {
if l == nil {
return nil
}
out := make(Labels, len(l))
for key, vals := range l {
cvals := make([]string, len(vals))
copy(cvals, vals)
out[key] = cvals
}
return out
} | [
"func",
"(",
"l",
"Labels",
")",
"Clone",
"(",
")",
"Labels",
"{",
"if",
"l",
"==",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"out",
":=",
"make",
"(",
"Labels",
",",
"len",
"(",
"l",
")",
")",
"\n",
"for",
"key",
",",
"vals",
":=",
"range",
"l",
"{",
"cvals",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"vals",
")",
")",
"\n",
"copy",
"(",
"cvals",
",",
"vals",
")",
"\n",
"out",
"[",
"key",
"]",
"=",
"cvals",
"\n",
"}",
"\n",
"return",
"out",
"\n",
"}"
] | // Clone returns non-shallow copy of the labels set | [
"Clone",
"returns",
"non",
"-",
"shallow",
"copy",
"of",
"the",
"labels",
"set"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1816-L1827 | train |
gravitational/teleport | lib/services/role.go | Equals | func (l Labels) Equals(o Labels) bool {
if len(l) != len(o) {
return false
}
for key := range l {
if !utils.StringSlicesEqual(l[key], o[key]) {
return false
}
}
return true
} | go | func (l Labels) Equals(o Labels) bool {
if len(l) != len(o) {
return false
}
for key := range l {
if !utils.StringSlicesEqual(l[key], o[key]) {
return false
}
}
return true
} | [
"func",
"(",
"l",
"Labels",
")",
"Equals",
"(",
"o",
"Labels",
")",
"bool",
"{",
"if",
"len",
"(",
"l",
")",
"!=",
"len",
"(",
"o",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"for",
"key",
":=",
"range",
"l",
"{",
"if",
"!",
"utils",
".",
"StringSlicesEqual",
"(",
"l",
"[",
"key",
"]",
",",
"o",
"[",
"key",
"]",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}"
] | // Equals returns true if two label sets are equal | [
"Equals",
"returns",
"true",
"if",
"two",
"label",
"sets",
"are",
"equal"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1830-L1840 | train |
gravitational/teleport | lib/services/role.go | MarshalTo | func (b BoolOption) MarshalTo(data []byte) (int, error) {
return b.protoType().MarshalTo(data)
} | go | func (b BoolOption) MarshalTo(data []byte) (int, error) {
return b.protoType().MarshalTo(data)
} | [
"func",
"(",
"b",
"BoolOption",
")",
"MarshalTo",
"(",
"data",
"[",
"]",
"byte",
")",
"(",
"int",
",",
"error",
")",
"{",
"return",
"b",
".",
"protoType",
"(",
")",
".",
"MarshalTo",
"(",
"data",
")",
"\n",
"}"
] | // MarshalTo marshals value to the slice | [
"MarshalTo",
"marshals",
"value",
"to",
"the",
"slice"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L1933-L1935 | train |
gravitational/teleport | lib/services/role.go | UnmarshalJSON | func (d *Duration) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
return nil
}
var stringVar string
if err := json.Unmarshal(data, &stringVar); err != nil {
return trace.Wrap(err)
}
if stringVar == teleport.DurationNever {
*d = Duration(0)
} else {
out, err := time.ParseDuration(stringVar)
if err != nil {
return trace.BadParameter(err.Error())
}
*d = Duration(out)
}
return nil
} | go | func (d *Duration) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
return nil
}
var stringVar string
if err := json.Unmarshal(data, &stringVar); err != nil {
return trace.Wrap(err)
}
if stringVar == teleport.DurationNever {
*d = Duration(0)
} else {
out, err := time.ParseDuration(stringVar)
if err != nil {
return trace.BadParameter(err.Error())
}
*d = Duration(out)
}
return nil
} | [
"func",
"(",
"d",
"*",
"Duration",
")",
"UnmarshalJSON",
"(",
"data",
"[",
"]",
"byte",
")",
"error",
"{",
"if",
"len",
"(",
"data",
")",
"==",
"0",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"var",
"stringVar",
"string",
"\n",
"if",
"err",
":=",
"json",
".",
"Unmarshal",
"(",
"data",
",",
"&",
"stringVar",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"if",
"stringVar",
"==",
"teleport",
".",
"DurationNever",
"{",
"*",
"d",
"=",
"Duration",
"(",
"0",
")",
"\n",
"}",
"else",
"{",
"out",
",",
"err",
":=",
"time",
".",
"ParseDuration",
"(",
"stringVar",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"BadParameter",
"(",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"\n",
"*",
"d",
"=",
"Duration",
"(",
"out",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // UnmarshalJSON marshals Duration to string | [
"UnmarshalJSON",
"marshals",
"Duration",
"to",
"string"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L2007-L2025 | train |
gravitational/teleport | lib/services/role.go | GetRoleSchema | func GetRoleSchema(version string, extensionSchema string) string {
schemaDefinitions := "," + RoleSpecV3SchemaDefinitions
if version == V2 {
schemaDefinitions = DefaultDefinitions
}
schemaTemplate := RoleSpecV3SchemaTemplate
if version == V2 {
schemaTemplate = RoleSpecV2SchemaTemplate
}
schema := fmt.Sprintf(schemaTemplate, ``)
if extensionSchema != "" {
schema = fmt.Sprintf(schemaTemplate, ","+extensionSchema)
}
return fmt.Sprintf(V2SchemaTemplate, MetadataSchema, schema, schemaDefinitions)
} | go | func GetRoleSchema(version string, extensionSchema string) string {
schemaDefinitions := "," + RoleSpecV3SchemaDefinitions
if version == V2 {
schemaDefinitions = DefaultDefinitions
}
schemaTemplate := RoleSpecV3SchemaTemplate
if version == V2 {
schemaTemplate = RoleSpecV2SchemaTemplate
}
schema := fmt.Sprintf(schemaTemplate, ``)
if extensionSchema != "" {
schema = fmt.Sprintf(schemaTemplate, ","+extensionSchema)
}
return fmt.Sprintf(V2SchemaTemplate, MetadataSchema, schema, schemaDefinitions)
} | [
"func",
"GetRoleSchema",
"(",
"version",
"string",
",",
"extensionSchema",
"string",
")",
"string",
"{",
"schemaDefinitions",
":=",
"\"",
"\"",
"+",
"RoleSpecV3SchemaDefinitions",
"\n",
"if",
"version",
"==",
"V2",
"{",
"schemaDefinitions",
"=",
"DefaultDefinitions",
"\n",
"}",
"\n\n",
"schemaTemplate",
":=",
"RoleSpecV3SchemaTemplate",
"\n",
"if",
"version",
"==",
"V2",
"{",
"schemaTemplate",
"=",
"RoleSpecV2SchemaTemplate",
"\n",
"}",
"\n\n",
"schema",
":=",
"fmt",
".",
"Sprintf",
"(",
"schemaTemplate",
",",
"``",
")",
"\n",
"if",
"extensionSchema",
"!=",
"\"",
"\"",
"{",
"schema",
"=",
"fmt",
".",
"Sprintf",
"(",
"schemaTemplate",
",",
"\"",
"\"",
"+",
"extensionSchema",
")",
"\n",
"}",
"\n\n",
"return",
"fmt",
".",
"Sprintf",
"(",
"V2SchemaTemplate",
",",
"MetadataSchema",
",",
"schema",
",",
"schemaDefinitions",
")",
"\n",
"}"
] | // GetRoleSchema returns role schema for the version requested with optionally
// injected schema for extensions. | [
"GetRoleSchema",
"returns",
"role",
"schema",
"for",
"the",
"version",
"requested",
"with",
"optionally",
"injected",
"schema",
"for",
"extensions",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L2158-L2175 | train |
gravitational/teleport | lib/services/role.go | UnmarshalRole | func UnmarshalRole(data []byte, opts ...MarshalOption) (*RoleV3, error) {
var h ResourceHeader
err := json.Unmarshal(data, &h)
if err != nil {
h.Version = V2
}
cfg, err := collectOptions(opts)
if err != nil {
return nil, trace.Wrap(err)
}
switch h.Version {
case V2:
var role RoleV2
if err := utils.UnmarshalWithSchema(GetRoleSchema(V2, ""), &role, data); err != nil {
return nil, trace.BadParameter(err.Error())
}
if err := role.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
roleV3 := role.V3()
roleV3.SetResourceID(cfg.ID)
return roleV3, nil
case V3:
var role RoleV3
if cfg.SkipValidation {
if err := utils.FastUnmarshal(data, &role); err != nil {
return nil, trace.BadParameter(err.Error())
}
} else {
if err := utils.UnmarshalWithSchema(GetRoleSchema(V3, ""), &role, data); err != nil {
return nil, trace.BadParameter(err.Error())
}
}
if err := role.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
if cfg.ID != 0 {
role.SetResourceID(cfg.ID)
}
if !cfg.Expires.IsZero() {
role.SetExpiry(cfg.Expires)
}
return &role, nil
}
return nil, trace.BadParameter("role version %q is not supported", h.Version)
} | go | func UnmarshalRole(data []byte, opts ...MarshalOption) (*RoleV3, error) {
var h ResourceHeader
err := json.Unmarshal(data, &h)
if err != nil {
h.Version = V2
}
cfg, err := collectOptions(opts)
if err != nil {
return nil, trace.Wrap(err)
}
switch h.Version {
case V2:
var role RoleV2
if err := utils.UnmarshalWithSchema(GetRoleSchema(V2, ""), &role, data); err != nil {
return nil, trace.BadParameter(err.Error())
}
if err := role.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
roleV3 := role.V3()
roleV3.SetResourceID(cfg.ID)
return roleV3, nil
case V3:
var role RoleV3
if cfg.SkipValidation {
if err := utils.FastUnmarshal(data, &role); err != nil {
return nil, trace.BadParameter(err.Error())
}
} else {
if err := utils.UnmarshalWithSchema(GetRoleSchema(V3, ""), &role, data); err != nil {
return nil, trace.BadParameter(err.Error())
}
}
if err := role.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
if cfg.ID != 0 {
role.SetResourceID(cfg.ID)
}
if !cfg.Expires.IsZero() {
role.SetExpiry(cfg.Expires)
}
return &role, nil
}
return nil, trace.BadParameter("role version %q is not supported", h.Version)
} | [
"func",
"UnmarshalRole",
"(",
"data",
"[",
"]",
"byte",
",",
"opts",
"...",
"MarshalOption",
")",
"(",
"*",
"RoleV3",
",",
"error",
")",
"{",
"var",
"h",
"ResourceHeader",
"\n",
"err",
":=",
"json",
".",
"Unmarshal",
"(",
"data",
",",
"&",
"h",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"h",
".",
"Version",
"=",
"V2",
"\n",
"}",
"\n\n",
"cfg",
",",
"err",
":=",
"collectOptions",
"(",
"opts",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"switch",
"h",
".",
"Version",
"{",
"case",
"V2",
":",
"var",
"role",
"RoleV2",
"\n",
"if",
"err",
":=",
"utils",
".",
"UnmarshalWithSchema",
"(",
"GetRoleSchema",
"(",
"V2",
",",
"\"",
"\"",
")",
",",
"&",
"role",
",",
"data",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"\n\n",
"if",
"err",
":=",
"role",
".",
"CheckAndSetDefaults",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"roleV3",
":=",
"role",
".",
"V3",
"(",
")",
"\n",
"roleV3",
".",
"SetResourceID",
"(",
"cfg",
".",
"ID",
")",
"\n",
"return",
"roleV3",
",",
"nil",
"\n",
"case",
"V3",
":",
"var",
"role",
"RoleV3",
"\n",
"if",
"cfg",
".",
"SkipValidation",
"{",
"if",
"err",
":=",
"utils",
".",
"FastUnmarshal",
"(",
"data",
",",
"&",
"role",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"\n",
"}",
"else",
"{",
"if",
"err",
":=",
"utils",
".",
"UnmarshalWithSchema",
"(",
"GetRoleSchema",
"(",
"V3",
",",
"\"",
"\"",
")",
",",
"&",
"role",
",",
"data",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"err",
":=",
"role",
".",
"CheckAndSetDefaults",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"if",
"cfg",
".",
"ID",
"!=",
"0",
"{",
"role",
".",
"SetResourceID",
"(",
"cfg",
".",
"ID",
")",
"\n",
"}",
"\n",
"if",
"!",
"cfg",
".",
"Expires",
".",
"IsZero",
"(",
")",
"{",
"role",
".",
"SetExpiry",
"(",
"cfg",
".",
"Expires",
")",
"\n",
"}",
"\n",
"return",
"&",
"role",
",",
"nil",
"\n",
"}",
"\n\n",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"h",
".",
"Version",
")",
"\n",
"}"
] | // UnmarshalRole unmarshals role from JSON, sets defaults, and checks schema. | [
"UnmarshalRole",
"unmarshals",
"role",
"from",
"JSON",
"sets",
"defaults",
"and",
"checks",
"schema",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L2178-L2230 | train |
gravitational/teleport | lib/services/role.go | UnmarshalRole | func (*TeleportRoleMarshaler) UnmarshalRole(bytes []byte, opts ...MarshalOption) (Role, error) {
return UnmarshalRole(bytes, opts...)
} | go | func (*TeleportRoleMarshaler) UnmarshalRole(bytes []byte, opts ...MarshalOption) (Role, error) {
return UnmarshalRole(bytes, opts...)
} | [
"func",
"(",
"*",
"TeleportRoleMarshaler",
")",
"UnmarshalRole",
"(",
"bytes",
"[",
"]",
"byte",
",",
"opts",
"...",
"MarshalOption",
")",
"(",
"Role",
",",
"error",
")",
"{",
"return",
"UnmarshalRole",
"(",
"bytes",
",",
"opts",
"...",
")",
"\n",
"}"
] | // UnmarshalRole unmarshals role from JSON. | [
"UnmarshalRole",
"unmarshals",
"role",
"from",
"JSON",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L2258-L2260 | train |
gravitational/teleport | lib/services/role.go | MarshalRole | func (*TeleportRoleMarshaler) MarshalRole(r Role, opts ...MarshalOption) ([]byte, error) {
cfg, err := collectOptions(opts)
if err != nil {
return nil, trace.Wrap(err)
}
switch role := r.(type) {
case *RoleV3:
if !cfg.PreserveResourceID {
// avoid modifying the original object
// to prevent unexpected data races
copy := *role
copy.SetResourceID(0)
role = ©
}
return utils.FastMarshal(role)
default:
return nil, trace.BadParameter("unrecognized role version %T", r)
}
} | go | func (*TeleportRoleMarshaler) MarshalRole(r Role, opts ...MarshalOption) ([]byte, error) {
cfg, err := collectOptions(opts)
if err != nil {
return nil, trace.Wrap(err)
}
switch role := r.(type) {
case *RoleV3:
if !cfg.PreserveResourceID {
// avoid modifying the original object
// to prevent unexpected data races
copy := *role
copy.SetResourceID(0)
role = ©
}
return utils.FastMarshal(role)
default:
return nil, trace.BadParameter("unrecognized role version %T", r)
}
} | [
"func",
"(",
"*",
"TeleportRoleMarshaler",
")",
"MarshalRole",
"(",
"r",
"Role",
",",
"opts",
"...",
"MarshalOption",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"cfg",
",",
"err",
":=",
"collectOptions",
"(",
"opts",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"switch",
"role",
":=",
"r",
".",
"(",
"type",
")",
"{",
"case",
"*",
"RoleV3",
":",
"if",
"!",
"cfg",
".",
"PreserveResourceID",
"{",
"// avoid modifying the original object",
"// to prevent unexpected data races",
"copy",
":=",
"*",
"role",
"\n",
"copy",
".",
"SetResourceID",
"(",
"0",
")",
"\n",
"role",
"=",
"&",
"copy",
"\n",
"}",
"\n",
"return",
"utils",
".",
"FastMarshal",
"(",
"role",
")",
"\n",
"default",
":",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"r",
")",
"\n",
"}",
"\n",
"}"
] | // MarshalRole marshalls role into JSON. | [
"MarshalRole",
"marshalls",
"role",
"into",
"JSON",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/services/role.go#L2263-L2281 | train |
gravitational/teleport | tool/tctl/common/resource_command.go | Initialize | func (g *ResourceCommand) Initialize(app *kingpin.Application, config *service.Config) {
g.CreateHandlers = map[ResourceKind]ResourceCreateHandler{
services.KindUser: g.createUser,
services.KindTrustedCluster: g.createTrustedCluster,
services.KindGithubConnector: g.createGithubConnector,
services.KindCertAuthority: g.createCertAuthority,
}
g.config = config
g.createCmd = app.Command("create", "Create or update a Teleport resource from a YAML file")
g.createCmd.Arg("filename", "resource definition file").Required().StringVar(&g.filename)
g.createCmd.Flag("force", "Overwrite the resource if already exists").Short('f').BoolVar(&g.force)
g.deleteCmd = app.Command("rm", "Delete a resource").Alias("del")
g.deleteCmd.Arg("resource", "Resource to delete").SetValue(&g.ref)
g.getCmd = app.Command("get", "Print a YAML declaration of various Teleport resources")
g.getCmd.Arg("resource", "Resource spec: 'type/[name]'").SetValue(&g.ref)
g.getCmd.Flag("format", "Output format: 'yaml', 'json' or 'text'").Default(formatYAML).StringVar(&g.format)
g.getCmd.Flag("namespace", "Namespace of the resources").Hidden().Default(defaults.Namespace).StringVar(&g.namespace)
g.getCmd.Flag("with-secrets", "Include secrets in resources like certificate authorities or OIDC connectors").Default("false").BoolVar(&g.withSecrets)
g.getCmd.Alias(getHelp)
} | go | func (g *ResourceCommand) Initialize(app *kingpin.Application, config *service.Config) {
g.CreateHandlers = map[ResourceKind]ResourceCreateHandler{
services.KindUser: g.createUser,
services.KindTrustedCluster: g.createTrustedCluster,
services.KindGithubConnector: g.createGithubConnector,
services.KindCertAuthority: g.createCertAuthority,
}
g.config = config
g.createCmd = app.Command("create", "Create or update a Teleport resource from a YAML file")
g.createCmd.Arg("filename", "resource definition file").Required().StringVar(&g.filename)
g.createCmd.Flag("force", "Overwrite the resource if already exists").Short('f').BoolVar(&g.force)
g.deleteCmd = app.Command("rm", "Delete a resource").Alias("del")
g.deleteCmd.Arg("resource", "Resource to delete").SetValue(&g.ref)
g.getCmd = app.Command("get", "Print a YAML declaration of various Teleport resources")
g.getCmd.Arg("resource", "Resource spec: 'type/[name]'").SetValue(&g.ref)
g.getCmd.Flag("format", "Output format: 'yaml', 'json' or 'text'").Default(formatYAML).StringVar(&g.format)
g.getCmd.Flag("namespace", "Namespace of the resources").Hidden().Default(defaults.Namespace).StringVar(&g.namespace)
g.getCmd.Flag("with-secrets", "Include secrets in resources like certificate authorities or OIDC connectors").Default("false").BoolVar(&g.withSecrets)
g.getCmd.Alias(getHelp)
} | [
"func",
"(",
"g",
"*",
"ResourceCommand",
")",
"Initialize",
"(",
"app",
"*",
"kingpin",
".",
"Application",
",",
"config",
"*",
"service",
".",
"Config",
")",
"{",
"g",
".",
"CreateHandlers",
"=",
"map",
"[",
"ResourceKind",
"]",
"ResourceCreateHandler",
"{",
"services",
".",
"KindUser",
":",
"g",
".",
"createUser",
",",
"services",
".",
"KindTrustedCluster",
":",
"g",
".",
"createTrustedCluster",
",",
"services",
".",
"KindGithubConnector",
":",
"g",
".",
"createGithubConnector",
",",
"services",
".",
"KindCertAuthority",
":",
"g",
".",
"createCertAuthority",
",",
"}",
"\n",
"g",
".",
"config",
"=",
"config",
"\n\n",
"g",
".",
"createCmd",
"=",
"app",
".",
"Command",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"g",
".",
"createCmd",
".",
"Arg",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
".",
"Required",
"(",
")",
".",
"StringVar",
"(",
"&",
"g",
".",
"filename",
")",
"\n",
"g",
".",
"createCmd",
".",
"Flag",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
".",
"Short",
"(",
"'f'",
")",
".",
"BoolVar",
"(",
"&",
"g",
".",
"force",
")",
"\n\n",
"g",
".",
"deleteCmd",
"=",
"app",
".",
"Command",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
".",
"Alias",
"(",
"\"",
"\"",
")",
"\n",
"g",
".",
"deleteCmd",
".",
"Arg",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
".",
"SetValue",
"(",
"&",
"g",
".",
"ref",
")",
"\n\n",
"g",
".",
"getCmd",
"=",
"app",
".",
"Command",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"g",
".",
"getCmd",
".",
"Arg",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
".",
"SetValue",
"(",
"&",
"g",
".",
"ref",
")",
"\n",
"g",
".",
"getCmd",
".",
"Flag",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
".",
"Default",
"(",
"formatYAML",
")",
".",
"StringVar",
"(",
"&",
"g",
".",
"format",
")",
"\n",
"g",
".",
"getCmd",
".",
"Flag",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
".",
"Hidden",
"(",
")",
".",
"Default",
"(",
"defaults",
".",
"Namespace",
")",
".",
"StringVar",
"(",
"&",
"g",
".",
"namespace",
")",
"\n",
"g",
".",
"getCmd",
".",
"Flag",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
".",
"Default",
"(",
"\"",
"\"",
")",
".",
"BoolVar",
"(",
"&",
"g",
".",
"withSecrets",
")",
"\n\n",
"g",
".",
"getCmd",
".",
"Alias",
"(",
"getHelp",
")",
"\n",
"}"
] | // Initialize allows ResourceCommand to plug itself into the CLI parser | [
"Initialize",
"allows",
"ResourceCommand",
"to",
"plug",
"itself",
"into",
"the",
"CLI",
"parser"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/tool/tctl/common/resource_command.go#L71-L94 | train |
gravitational/teleport | tool/tctl/common/resource_command.go | IsDeleteSubcommand | func (g *ResourceCommand) IsDeleteSubcommand(cmd string) bool {
return cmd == g.deleteCmd.FullCommand()
} | go | func (g *ResourceCommand) IsDeleteSubcommand(cmd string) bool {
return cmd == g.deleteCmd.FullCommand()
} | [
"func",
"(",
"g",
"*",
"ResourceCommand",
")",
"IsDeleteSubcommand",
"(",
"cmd",
"string",
")",
"bool",
"{",
"return",
"cmd",
"==",
"g",
".",
"deleteCmd",
".",
"FullCommand",
"(",
")",
"\n",
"}"
] | // IsDeleteSubcommand returns 'true' if the given command is `tctl rm` | [
"IsDeleteSubcommand",
"returns",
"true",
"if",
"the",
"given",
"command",
"is",
"tctl",
"rm"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/tool/tctl/common/resource_command.go#L116-L118 | train |
gravitational/teleport | tool/tctl/common/resource_command.go | Get | func (g *ResourceCommand) Get(client auth.ClientI) error {
collection, err := g.getCollection(client)
if err != nil {
return trace.Wrap(err)
}
// Note that only YAML is officially supported. Support for text and JSON
// is experimental.
switch g.format {
case teleport.YAML:
return collection.writeYAML(os.Stdout)
case teleport.Text:
return collection.writeText(os.Stdout)
case teleport.JSON:
return collection.writeJSON(os.Stdout)
}
return trace.BadParameter("unsupported format")
} | go | func (g *ResourceCommand) Get(client auth.ClientI) error {
collection, err := g.getCollection(client)
if err != nil {
return trace.Wrap(err)
}
// Note that only YAML is officially supported. Support for text and JSON
// is experimental.
switch g.format {
case teleport.YAML:
return collection.writeYAML(os.Stdout)
case teleport.Text:
return collection.writeText(os.Stdout)
case teleport.JSON:
return collection.writeJSON(os.Stdout)
}
return trace.BadParameter("unsupported format")
} | [
"func",
"(",
"g",
"*",
"ResourceCommand",
")",
"Get",
"(",
"client",
"auth",
".",
"ClientI",
")",
"error",
"{",
"collection",
",",
"err",
":=",
"g",
".",
"getCollection",
"(",
"client",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"// Note that only YAML is officially supported. Support for text and JSON",
"// is experimental.",
"switch",
"g",
".",
"format",
"{",
"case",
"teleport",
".",
"YAML",
":",
"return",
"collection",
".",
"writeYAML",
"(",
"os",
".",
"Stdout",
")",
"\n",
"case",
"teleport",
".",
"Text",
":",
"return",
"collection",
".",
"writeText",
"(",
"os",
".",
"Stdout",
")",
"\n",
"case",
"teleport",
".",
"JSON",
":",
"return",
"collection",
".",
"writeJSON",
"(",
"os",
".",
"Stdout",
")",
"\n",
"}",
"\n",
"return",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}"
] | // Get prints one or many resources of a certain type | [
"Get",
"prints",
"one",
"or",
"many",
"resources",
"of",
"a",
"certain",
"type"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/tool/tctl/common/resource_command.go#L127-L144 | train |
gravitational/teleport | tool/tctl/common/resource_command.go | Create | func (u *ResourceCommand) Create(client auth.ClientI) error {
reader, err := utils.OpenFile(u.filename)
if err != nil {
return trace.Wrap(err)
}
decoder := kyaml.NewYAMLOrJSONDecoder(reader, 32*1024)
count := 0
for {
var raw services.UnknownResource
err := decoder.Decode(&raw)
if err != nil {
if err == io.EOF {
if count == 0 {
return trace.BadParameter("no resources found, empty input?")
}
return nil
}
return trace.Wrap(err)
}
count++
// locate the creator function for a given resource kind:
creator, found := u.CreateHandlers[ResourceKind(raw.Kind)]
if !found {
return trace.BadParameter("creating resources of type %q is not supported", raw.Kind)
}
// only return in case of error, to create multiple resources
// in case if yaml spec is a list
if err := creator(client, raw); err != nil {
return trace.Wrap(err)
}
}
} | go | func (u *ResourceCommand) Create(client auth.ClientI) error {
reader, err := utils.OpenFile(u.filename)
if err != nil {
return trace.Wrap(err)
}
decoder := kyaml.NewYAMLOrJSONDecoder(reader, 32*1024)
count := 0
for {
var raw services.UnknownResource
err := decoder.Decode(&raw)
if err != nil {
if err == io.EOF {
if count == 0 {
return trace.BadParameter("no resources found, empty input?")
}
return nil
}
return trace.Wrap(err)
}
count++
// locate the creator function for a given resource kind:
creator, found := u.CreateHandlers[ResourceKind(raw.Kind)]
if !found {
return trace.BadParameter("creating resources of type %q is not supported", raw.Kind)
}
// only return in case of error, to create multiple resources
// in case if yaml spec is a list
if err := creator(client, raw); err != nil {
return trace.Wrap(err)
}
}
} | [
"func",
"(",
"u",
"*",
"ResourceCommand",
")",
"Create",
"(",
"client",
"auth",
".",
"ClientI",
")",
"error",
"{",
"reader",
",",
"err",
":=",
"utils",
".",
"OpenFile",
"(",
"u",
".",
"filename",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"decoder",
":=",
"kyaml",
".",
"NewYAMLOrJSONDecoder",
"(",
"reader",
",",
"32",
"*",
"1024",
")",
"\n",
"count",
":=",
"0",
"\n",
"for",
"{",
"var",
"raw",
"services",
".",
"UnknownResource",
"\n",
"err",
":=",
"decoder",
".",
"Decode",
"(",
"&",
"raw",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"if",
"err",
"==",
"io",
".",
"EOF",
"{",
"if",
"count",
"==",
"0",
"{",
"return",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"count",
"++",
"\n\n",
"// locate the creator function for a given resource kind:",
"creator",
",",
"found",
":=",
"u",
".",
"CreateHandlers",
"[",
"ResourceKind",
"(",
"raw",
".",
"Kind",
")",
"]",
"\n",
"if",
"!",
"found",
"{",
"return",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"raw",
".",
"Kind",
")",
"\n",
"}",
"\n",
"// only return in case of error, to create multiple resources",
"// in case if yaml spec is a list",
"if",
"err",
":=",
"creator",
"(",
"client",
",",
"raw",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // Create updates or insterts one or many resources | [
"Create",
"updates",
"or",
"insterts",
"one",
"or",
"many",
"resources"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/tool/tctl/common/resource_command.go#L147-L179 | train |
gravitational/teleport | tool/tctl/common/resource_command.go | createTrustedCluster | func (u *ResourceCommand) createTrustedCluster(client auth.ClientI, raw services.UnknownResource) error {
tc, err := services.GetTrustedClusterMarshaler().Unmarshal(raw.Raw)
if err != nil {
return trace.Wrap(err)
}
// check if such cluster already exists:
name := tc.GetName()
_, err = client.GetTrustedCluster(name)
if err != nil && !trace.IsNotFound(err) {
return trace.Wrap(err)
}
exists := (err == nil)
if u.force == false && exists {
return trace.AlreadyExists("trusted cluster '%s' already exists", name)
}
out, err := client.UpsertTrustedCluster(tc)
if err != nil {
// If force is used and UpsertTrustedCluster returns trace.AlreadyExists,
// this means the user tried to upsert a cluster whose exact match already
// exists in the backend, nothing needs to occur other than happy message
// that the trusted cluster has been created.
if u.force && trace.IsAlreadyExists(err) {
out = tc
} else {
return trace.Wrap(err)
}
}
if out.GetName() != tc.GetName() {
fmt.Printf("WARNING: trusted cluster %q resource has been renamed to match remote cluster name %q\n", name, out.GetName())
}
fmt.Printf("trusted cluster %q has been %v\n", out.GetName(), UpsertVerb(exists, u.force))
return nil
} | go | func (u *ResourceCommand) createTrustedCluster(client auth.ClientI, raw services.UnknownResource) error {
tc, err := services.GetTrustedClusterMarshaler().Unmarshal(raw.Raw)
if err != nil {
return trace.Wrap(err)
}
// check if such cluster already exists:
name := tc.GetName()
_, err = client.GetTrustedCluster(name)
if err != nil && !trace.IsNotFound(err) {
return trace.Wrap(err)
}
exists := (err == nil)
if u.force == false && exists {
return trace.AlreadyExists("trusted cluster '%s' already exists", name)
}
out, err := client.UpsertTrustedCluster(tc)
if err != nil {
// If force is used and UpsertTrustedCluster returns trace.AlreadyExists,
// this means the user tried to upsert a cluster whose exact match already
// exists in the backend, nothing needs to occur other than happy message
// that the trusted cluster has been created.
if u.force && trace.IsAlreadyExists(err) {
out = tc
} else {
return trace.Wrap(err)
}
}
if out.GetName() != tc.GetName() {
fmt.Printf("WARNING: trusted cluster %q resource has been renamed to match remote cluster name %q\n", name, out.GetName())
}
fmt.Printf("trusted cluster %q has been %v\n", out.GetName(), UpsertVerb(exists, u.force))
return nil
} | [
"func",
"(",
"u",
"*",
"ResourceCommand",
")",
"createTrustedCluster",
"(",
"client",
"auth",
".",
"ClientI",
",",
"raw",
"services",
".",
"UnknownResource",
")",
"error",
"{",
"tc",
",",
"err",
":=",
"services",
".",
"GetTrustedClusterMarshaler",
"(",
")",
".",
"Unmarshal",
"(",
"raw",
".",
"Raw",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"// check if such cluster already exists:",
"name",
":=",
"tc",
".",
"GetName",
"(",
")",
"\n",
"_",
",",
"err",
"=",
"client",
".",
"GetTrustedCluster",
"(",
"name",
")",
"\n",
"if",
"err",
"!=",
"nil",
"&&",
"!",
"trace",
".",
"IsNotFound",
"(",
"err",
")",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"exists",
":=",
"(",
"err",
"==",
"nil",
")",
"\n",
"if",
"u",
".",
"force",
"==",
"false",
"&&",
"exists",
"{",
"return",
"trace",
".",
"AlreadyExists",
"(",
"\"",
"\"",
",",
"name",
")",
"\n",
"}",
"\n\n",
"out",
",",
"err",
":=",
"client",
".",
"UpsertTrustedCluster",
"(",
"tc",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"// If force is used and UpsertTrustedCluster returns trace.AlreadyExists,",
"// this means the user tried to upsert a cluster whose exact match already",
"// exists in the backend, nothing needs to occur other than happy message",
"// that the trusted cluster has been created.",
"if",
"u",
".",
"force",
"&&",
"trace",
".",
"IsAlreadyExists",
"(",
"err",
")",
"{",
"out",
"=",
"tc",
"\n",
"}",
"else",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"out",
".",
"GetName",
"(",
")",
"!=",
"tc",
".",
"GetName",
"(",
")",
"{",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"name",
",",
"out",
".",
"GetName",
"(",
")",
")",
"\n",
"}",
"\n",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"out",
".",
"GetName",
"(",
")",
",",
"UpsertVerb",
"(",
"exists",
",",
"u",
".",
"force",
")",
")",
"\n",
"return",
"nil",
"\n",
"}"
] | // createTrustedCluster implements `tctl create cluster.yaml` command | [
"createTrustedCluster",
"implements",
"tctl",
"create",
"cluster",
".",
"yaml",
"command"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/tool/tctl/common/resource_command.go#L182-L216 | train |
gravitational/teleport | tool/tctl/common/resource_command.go | createCertAuthority | func (u *ResourceCommand) createCertAuthority(client auth.ClientI, raw services.UnknownResource) error {
certAuthority, err := services.GetCertAuthorityMarshaler().UnmarshalCertAuthority(raw.Raw)
if err != nil {
return trace.Wrap(err)
}
if err := client.UpsertCertAuthority(certAuthority); err != nil {
return trace.Wrap(err)
}
fmt.Printf("certificate authority '%s' has been updated\n", certAuthority.GetName())
return nil
} | go | func (u *ResourceCommand) createCertAuthority(client auth.ClientI, raw services.UnknownResource) error {
certAuthority, err := services.GetCertAuthorityMarshaler().UnmarshalCertAuthority(raw.Raw)
if err != nil {
return trace.Wrap(err)
}
if err := client.UpsertCertAuthority(certAuthority); err != nil {
return trace.Wrap(err)
}
fmt.Printf("certificate authority '%s' has been updated\n", certAuthority.GetName())
return nil
} | [
"func",
"(",
"u",
"*",
"ResourceCommand",
")",
"createCertAuthority",
"(",
"client",
"auth",
".",
"ClientI",
",",
"raw",
"services",
".",
"UnknownResource",
")",
"error",
"{",
"certAuthority",
",",
"err",
":=",
"services",
".",
"GetCertAuthorityMarshaler",
"(",
")",
".",
"UnmarshalCertAuthority",
"(",
"raw",
".",
"Raw",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"if",
"err",
":=",
"client",
".",
"UpsertCertAuthority",
"(",
"certAuthority",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"certAuthority",
".",
"GetName",
"(",
")",
")",
"\n",
"return",
"nil",
"\n",
"}"
] | // createCertAuthority creates certificate authority | [
"createCertAuthority",
"creates",
"certificate",
"authority"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/tool/tctl/common/resource_command.go#L219-L229 | train |
gravitational/teleport | tool/tctl/common/resource_command.go | createUser | func (u *ResourceCommand) createUser(client auth.ClientI, raw services.UnknownResource) error {
user, err := services.GetUserMarshaler().UnmarshalUser(raw.Raw)
if err != nil {
return trace.Wrap(err)
}
userName := user.GetName()
if err := client.UpsertUser(user); err != nil {
return trace.Wrap(err)
}
fmt.Printf("user '%s' has been updated\n", userName)
return nil
} | go | func (u *ResourceCommand) createUser(client auth.ClientI, raw services.UnknownResource) error {
user, err := services.GetUserMarshaler().UnmarshalUser(raw.Raw)
if err != nil {
return trace.Wrap(err)
}
userName := user.GetName()
if err := client.UpsertUser(user); err != nil {
return trace.Wrap(err)
}
fmt.Printf("user '%s' has been updated\n", userName)
return nil
} | [
"func",
"(",
"u",
"*",
"ResourceCommand",
")",
"createUser",
"(",
"client",
"auth",
".",
"ClientI",
",",
"raw",
"services",
".",
"UnknownResource",
")",
"error",
"{",
"user",
",",
"err",
":=",
"services",
".",
"GetUserMarshaler",
"(",
")",
".",
"UnmarshalUser",
"(",
"raw",
".",
"Raw",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"userName",
":=",
"user",
".",
"GetName",
"(",
")",
"\n",
"if",
"err",
":=",
"client",
".",
"UpsertUser",
"(",
"user",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"userName",
")",
"\n",
"return",
"nil",
"\n",
"}"
] | // createUser implements 'tctl create user.yaml' command | [
"createUser",
"implements",
"tctl",
"create",
"user",
".",
"yaml",
"command"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/tool/tctl/common/resource_command.go#L255-L266 | train |
gravitational/teleport | tool/tctl/common/resource_command.go | Delete | func (d *ResourceCommand) Delete(client auth.ClientI) (err error) {
if d.ref.Kind == "" || d.ref.Name == "" {
return trace.BadParameter("provide a full resource name to delete, for example:\n$ tctl rm cluster/east\n")
}
switch d.ref.Kind {
case services.KindNode:
if err = client.DeleteNode(defaults.Namespace, d.ref.Name); err != nil {
return trace.Wrap(err)
}
fmt.Printf("node %v has been deleted\n", d.ref.Name)
case services.KindUser:
if err = client.DeleteUser(d.ref.Name); err != nil {
return trace.Wrap(err)
}
fmt.Printf("user %v has been deleted\n", d.ref.Name)
case services.KindSAMLConnector:
if err = client.DeleteSAMLConnector(d.ref.Name); err != nil {
return trace.Wrap(err)
}
fmt.Printf("SAML Connector %v has been deleted\n", d.ref.Name)
case services.KindOIDCConnector:
if err = client.DeleteOIDCConnector(d.ref.Name); err != nil {
return trace.Wrap(err)
}
fmt.Printf("OIDC Connector %v has been deleted\n", d.ref.Name)
case services.KindGithubConnector:
if err = client.DeleteGithubConnector(d.ref.Name); err != nil {
return trace.Wrap(err)
}
fmt.Printf("github connector %q has been deleted\n", d.ref.Name)
case services.KindReverseTunnel:
if err := client.DeleteReverseTunnel(d.ref.Name); err != nil {
return trace.Wrap(err)
}
fmt.Printf("reverse tunnel %v has been deleted\n", d.ref.Name)
case services.KindTrustedCluster:
if err = client.DeleteTrustedCluster(d.ref.Name); err != nil {
return trace.Wrap(err)
}
fmt.Printf("trusted cluster %q has been deleted\n", d.ref.Name)
case services.KindRemoteCluster:
if err = client.DeleteRemoteCluster(d.ref.Name); err != nil {
return trace.Wrap(err)
}
fmt.Printf("remote cluster %q has been deleted\n", d.ref.Name)
default:
return trace.BadParameter("deleting resources of type %q is not supported", d.ref.Kind)
}
return nil
} | go | func (d *ResourceCommand) Delete(client auth.ClientI) (err error) {
if d.ref.Kind == "" || d.ref.Name == "" {
return trace.BadParameter("provide a full resource name to delete, for example:\n$ tctl rm cluster/east\n")
}
switch d.ref.Kind {
case services.KindNode:
if err = client.DeleteNode(defaults.Namespace, d.ref.Name); err != nil {
return trace.Wrap(err)
}
fmt.Printf("node %v has been deleted\n", d.ref.Name)
case services.KindUser:
if err = client.DeleteUser(d.ref.Name); err != nil {
return trace.Wrap(err)
}
fmt.Printf("user %v has been deleted\n", d.ref.Name)
case services.KindSAMLConnector:
if err = client.DeleteSAMLConnector(d.ref.Name); err != nil {
return trace.Wrap(err)
}
fmt.Printf("SAML Connector %v has been deleted\n", d.ref.Name)
case services.KindOIDCConnector:
if err = client.DeleteOIDCConnector(d.ref.Name); err != nil {
return trace.Wrap(err)
}
fmt.Printf("OIDC Connector %v has been deleted\n", d.ref.Name)
case services.KindGithubConnector:
if err = client.DeleteGithubConnector(d.ref.Name); err != nil {
return trace.Wrap(err)
}
fmt.Printf("github connector %q has been deleted\n", d.ref.Name)
case services.KindReverseTunnel:
if err := client.DeleteReverseTunnel(d.ref.Name); err != nil {
return trace.Wrap(err)
}
fmt.Printf("reverse tunnel %v has been deleted\n", d.ref.Name)
case services.KindTrustedCluster:
if err = client.DeleteTrustedCluster(d.ref.Name); err != nil {
return trace.Wrap(err)
}
fmt.Printf("trusted cluster %q has been deleted\n", d.ref.Name)
case services.KindRemoteCluster:
if err = client.DeleteRemoteCluster(d.ref.Name); err != nil {
return trace.Wrap(err)
}
fmt.Printf("remote cluster %q has been deleted\n", d.ref.Name)
default:
return trace.BadParameter("deleting resources of type %q is not supported", d.ref.Kind)
}
return nil
} | [
"func",
"(",
"d",
"*",
"ResourceCommand",
")",
"Delete",
"(",
"client",
"auth",
".",
"ClientI",
")",
"(",
"err",
"error",
")",
"{",
"if",
"d",
".",
"ref",
".",
"Kind",
"==",
"\"",
"\"",
"||",
"d",
".",
"ref",
".",
"Name",
"==",
"\"",
"\"",
"{",
"return",
"trace",
".",
"BadParameter",
"(",
"\"",
"\\n",
"\\n",
"\"",
")",
"\n",
"}",
"\n\n",
"switch",
"d",
".",
"ref",
".",
"Kind",
"{",
"case",
"services",
".",
"KindNode",
":",
"if",
"err",
"=",
"client",
".",
"DeleteNode",
"(",
"defaults",
".",
"Namespace",
",",
"d",
".",
"ref",
".",
"Name",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"d",
".",
"ref",
".",
"Name",
")",
"\n",
"case",
"services",
".",
"KindUser",
":",
"if",
"err",
"=",
"client",
".",
"DeleteUser",
"(",
"d",
".",
"ref",
".",
"Name",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"d",
".",
"ref",
".",
"Name",
")",
"\n",
"case",
"services",
".",
"KindSAMLConnector",
":",
"if",
"err",
"=",
"client",
".",
"DeleteSAMLConnector",
"(",
"d",
".",
"ref",
".",
"Name",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"d",
".",
"ref",
".",
"Name",
")",
"\n",
"case",
"services",
".",
"KindOIDCConnector",
":",
"if",
"err",
"=",
"client",
".",
"DeleteOIDCConnector",
"(",
"d",
".",
"ref",
".",
"Name",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"d",
".",
"ref",
".",
"Name",
")",
"\n",
"case",
"services",
".",
"KindGithubConnector",
":",
"if",
"err",
"=",
"client",
".",
"DeleteGithubConnector",
"(",
"d",
".",
"ref",
".",
"Name",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"d",
".",
"ref",
".",
"Name",
")",
"\n",
"case",
"services",
".",
"KindReverseTunnel",
":",
"if",
"err",
":=",
"client",
".",
"DeleteReverseTunnel",
"(",
"d",
".",
"ref",
".",
"Name",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"d",
".",
"ref",
".",
"Name",
")",
"\n",
"case",
"services",
".",
"KindTrustedCluster",
":",
"if",
"err",
"=",
"client",
".",
"DeleteTrustedCluster",
"(",
"d",
".",
"ref",
".",
"Name",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"d",
".",
"ref",
".",
"Name",
")",
"\n",
"case",
"services",
".",
"KindRemoteCluster",
":",
"if",
"err",
"=",
"client",
".",
"DeleteRemoteCluster",
"(",
"d",
".",
"ref",
".",
"Name",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"d",
".",
"ref",
".",
"Name",
")",
"\n",
"default",
":",
"return",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
",",
"d",
".",
"ref",
".",
"Kind",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // Delete deletes resource by name | [
"Delete",
"deletes",
"resource",
"by",
"name"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/tool/tctl/common/resource_command.go#L269-L319 | train |
gravitational/teleport | lib/reversetunnel/cache.go | NewHostCertificateCache | func NewHostCertificateCache(keygen sshca.Authority, authClient auth.ClientI) (*certificateCache, error) {
cache, err := ttlmap.New(defaults.HostCertCacheSize)
if err != nil {
return nil, trace.Wrap(err)
}
return &certificateCache{
keygen: keygen,
cache: cache,
authClient: authClient,
}, nil
} | go | func NewHostCertificateCache(keygen sshca.Authority, authClient auth.ClientI) (*certificateCache, error) {
cache, err := ttlmap.New(defaults.HostCertCacheSize)
if err != nil {
return nil, trace.Wrap(err)
}
return &certificateCache{
keygen: keygen,
cache: cache,
authClient: authClient,
}, nil
} | [
"func",
"NewHostCertificateCache",
"(",
"keygen",
"sshca",
".",
"Authority",
",",
"authClient",
"auth",
".",
"ClientI",
")",
"(",
"*",
"certificateCache",
",",
"error",
")",
"{",
"cache",
",",
"err",
":=",
"ttlmap",
".",
"New",
"(",
"defaults",
".",
"HostCertCacheSize",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"return",
"&",
"certificateCache",
"{",
"keygen",
":",
"keygen",
",",
"cache",
":",
"cache",
",",
"authClient",
":",
"authClient",
",",
"}",
",",
"nil",
"\n",
"}"
] | // NewHostCertificateCache creates a shared host certificate cache that is
// used by the forwarding server. | [
"NewHostCertificateCache",
"creates",
"a",
"shared",
"host",
"certificate",
"cache",
"that",
"is",
"used",
"by",
"the",
"forwarding",
"server",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/reversetunnel/cache.go#L45-L56 | train |
gravitational/teleport | lib/reversetunnel/cache.go | GetHostCertificate | func (c *certificateCache) GetHostCertificate(addr string, additionalPrincipals []string) (ssh.Signer, error) {
var certificate ssh.Signer
var err error
var ok bool
var principals []string
principals = append(principals, addr)
principals = append(principals, additionalPrincipals...)
certificate, ok = c.get(strings.Join(principals, "."))
if !ok {
certificate, err = c.generateHostCert(principals)
if err != nil {
return nil, trace.Wrap(err)
}
err = c.set(addr, certificate, defaults.HostCertCacheTime)
if err != nil {
return nil, trace.Wrap(err)
}
}
return certificate, nil
} | go | func (c *certificateCache) GetHostCertificate(addr string, additionalPrincipals []string) (ssh.Signer, error) {
var certificate ssh.Signer
var err error
var ok bool
var principals []string
principals = append(principals, addr)
principals = append(principals, additionalPrincipals...)
certificate, ok = c.get(strings.Join(principals, "."))
if !ok {
certificate, err = c.generateHostCert(principals)
if err != nil {
return nil, trace.Wrap(err)
}
err = c.set(addr, certificate, defaults.HostCertCacheTime)
if err != nil {
return nil, trace.Wrap(err)
}
}
return certificate, nil
} | [
"func",
"(",
"c",
"*",
"certificateCache",
")",
"GetHostCertificate",
"(",
"addr",
"string",
",",
"additionalPrincipals",
"[",
"]",
"string",
")",
"(",
"ssh",
".",
"Signer",
",",
"error",
")",
"{",
"var",
"certificate",
"ssh",
".",
"Signer",
"\n",
"var",
"err",
"error",
"\n",
"var",
"ok",
"bool",
"\n\n",
"var",
"principals",
"[",
"]",
"string",
"\n",
"principals",
"=",
"append",
"(",
"principals",
",",
"addr",
")",
"\n",
"principals",
"=",
"append",
"(",
"principals",
",",
"additionalPrincipals",
"...",
")",
"\n\n",
"certificate",
",",
"ok",
"=",
"c",
".",
"get",
"(",
"strings",
".",
"Join",
"(",
"principals",
",",
"\"",
"\"",
")",
")",
"\n",
"if",
"!",
"ok",
"{",
"certificate",
",",
"err",
"=",
"c",
".",
"generateHostCert",
"(",
"principals",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"err",
"=",
"c",
".",
"set",
"(",
"addr",
",",
"certificate",
",",
"defaults",
".",
"HostCertCacheTime",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"certificate",
",",
"nil",
"\n",
"}"
] | // GetHostCertificate will fetch a certificate from the cache. If the certificate
// is not in the cache, it will be generated, put in the cache, and returned. Mul
// Multiple callers can arrive and generate a host certificate at the same time.
// This is a tradeoff to prevent long delays here due to the expensive
// certificate generation call. | [
"GetHostCertificate",
"will",
"fetch",
"a",
"certificate",
"from",
"the",
"cache",
".",
"If",
"the",
"certificate",
"is",
"not",
"in",
"the",
"cache",
"it",
"will",
"be",
"generated",
"put",
"in",
"the",
"cache",
"and",
"returned",
".",
"Mul",
"Multiple",
"callers",
"can",
"arrive",
"and",
"generate",
"a",
"host",
"certificate",
"at",
"the",
"same",
"time",
".",
"This",
"is",
"a",
"tradeoff",
"to",
"prevent",
"long",
"delays",
"here",
"due",
"to",
"the",
"expensive",
"certificate",
"generation",
"call",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/reversetunnel/cache.go#L63-L86 | train |
gravitational/teleport | lib/reversetunnel/cache.go | get | func (c *certificateCache) get(addr string) (ssh.Signer, bool) {
c.mu.Lock()
defer c.mu.Unlock()
certificate, ok := c.cache.Get(addr)
if !ok {
return nil, false
}
certificateSigner, ok := certificate.(ssh.Signer)
if !ok {
return nil, false
}
return certificateSigner, true
} | go | func (c *certificateCache) get(addr string) (ssh.Signer, bool) {
c.mu.Lock()
defer c.mu.Unlock()
certificate, ok := c.cache.Get(addr)
if !ok {
return nil, false
}
certificateSigner, ok := certificate.(ssh.Signer)
if !ok {
return nil, false
}
return certificateSigner, true
} | [
"func",
"(",
"c",
"*",
"certificateCache",
")",
"get",
"(",
"addr",
"string",
")",
"(",
"ssh",
".",
"Signer",
",",
"bool",
")",
"{",
"c",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"defer",
"c",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n\n",
"certificate",
",",
"ok",
":=",
"c",
".",
"cache",
".",
"Get",
"(",
"addr",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"false",
"\n",
"}",
"\n\n",
"certificateSigner",
",",
"ok",
":=",
"certificate",
".",
"(",
"ssh",
".",
"Signer",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"false",
"\n",
"}",
"\n\n",
"return",
"certificateSigner",
",",
"true",
"\n",
"}"
] | // get is goroutine safe and will return a ssh.Signer for a principal from
// the cache. | [
"get",
"is",
"goroutine",
"safe",
"and",
"will",
"return",
"a",
"ssh",
".",
"Signer",
"for",
"a",
"principal",
"from",
"the",
"cache",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/reversetunnel/cache.go#L90-L105 | train |
gravitational/teleport | lib/reversetunnel/cache.go | set | func (c *certificateCache) set(addr string, certificate ssh.Signer, ttl time.Duration) error {
c.mu.Lock()
defer c.mu.Unlock()
err := c.cache.Set(addr, certificate, ttl)
if err != nil {
return trace.Wrap(err)
}
return nil
} | go | func (c *certificateCache) set(addr string, certificate ssh.Signer, ttl time.Duration) error {
c.mu.Lock()
defer c.mu.Unlock()
err := c.cache.Set(addr, certificate, ttl)
if err != nil {
return trace.Wrap(err)
}
return nil
} | [
"func",
"(",
"c",
"*",
"certificateCache",
")",
"set",
"(",
"addr",
"string",
",",
"certificate",
"ssh",
".",
"Signer",
",",
"ttl",
"time",
".",
"Duration",
")",
"error",
"{",
"c",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"defer",
"c",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n\n",
"err",
":=",
"c",
".",
"cache",
".",
"Set",
"(",
"addr",
",",
"certificate",
",",
"ttl",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] | // set is goroutine safe and will set a ssh.Signer for a principal in
// the cache. | [
"set",
"is",
"goroutine",
"safe",
"and",
"will",
"set",
"a",
"ssh",
".",
"Signer",
"for",
"a",
"principal",
"in",
"the",
"cache",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/reversetunnel/cache.go#L109-L119 | train |
gravitational/teleport | lib/reversetunnel/cache.go | generateHostCert | func (c *certificateCache) generateHostCert(principals []string) (ssh.Signer, error) {
if len(principals) == 0 {
return nil, trace.BadParameter("at least one principal must be provided")
}
// Generate public/private keypair.
privBytes, pubBytes, err := c.keygen.GetNewKeyPairFromPool()
if err != nil {
return nil, trace.Wrap(err)
}
// Generate a SSH host certificate.
clusterName, err := c.authClient.GetDomainName()
if err != nil {
return nil, trace.Wrap(err)
}
certBytes, err := c.authClient.GenerateHostCert(
pubBytes,
principals[0],
principals[0],
principals,
clusterName,
teleport.Roles{teleport.RoleNode},
0)
if err != nil {
return nil, trace.Wrap(err)
}
// create a *ssh.Certificate
privateKey, err := ssh.ParsePrivateKey(privBytes)
if err != nil {
return nil, trace.Wrap(err)
}
publicKey, _, _, _, err := ssh.ParseAuthorizedKey(certBytes)
if err != nil {
return nil, err
}
cert, ok := publicKey.(*ssh.Certificate)
if !ok {
return nil, trace.BadParameter("not a certificate")
}
// return a ssh.Signer
s, err := ssh.NewCertSigner(cert, privateKey)
if err != nil {
return nil, trace.Wrap(err)
}
return s, nil
} | go | func (c *certificateCache) generateHostCert(principals []string) (ssh.Signer, error) {
if len(principals) == 0 {
return nil, trace.BadParameter("at least one principal must be provided")
}
// Generate public/private keypair.
privBytes, pubBytes, err := c.keygen.GetNewKeyPairFromPool()
if err != nil {
return nil, trace.Wrap(err)
}
// Generate a SSH host certificate.
clusterName, err := c.authClient.GetDomainName()
if err != nil {
return nil, trace.Wrap(err)
}
certBytes, err := c.authClient.GenerateHostCert(
pubBytes,
principals[0],
principals[0],
principals,
clusterName,
teleport.Roles{teleport.RoleNode},
0)
if err != nil {
return nil, trace.Wrap(err)
}
// create a *ssh.Certificate
privateKey, err := ssh.ParsePrivateKey(privBytes)
if err != nil {
return nil, trace.Wrap(err)
}
publicKey, _, _, _, err := ssh.ParseAuthorizedKey(certBytes)
if err != nil {
return nil, err
}
cert, ok := publicKey.(*ssh.Certificate)
if !ok {
return nil, trace.BadParameter("not a certificate")
}
// return a ssh.Signer
s, err := ssh.NewCertSigner(cert, privateKey)
if err != nil {
return nil, trace.Wrap(err)
}
return s, nil
} | [
"func",
"(",
"c",
"*",
"certificateCache",
")",
"generateHostCert",
"(",
"principals",
"[",
"]",
"string",
")",
"(",
"ssh",
".",
"Signer",
",",
"error",
")",
"{",
"if",
"len",
"(",
"principals",
")",
"==",
"0",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"// Generate public/private keypair.",
"privBytes",
",",
"pubBytes",
",",
"err",
":=",
"c",
".",
"keygen",
".",
"GetNewKeyPairFromPool",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"// Generate a SSH host certificate.",
"clusterName",
",",
"err",
":=",
"c",
".",
"authClient",
".",
"GetDomainName",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"certBytes",
",",
"err",
":=",
"c",
".",
"authClient",
".",
"GenerateHostCert",
"(",
"pubBytes",
",",
"principals",
"[",
"0",
"]",
",",
"principals",
"[",
"0",
"]",
",",
"principals",
",",
"clusterName",
",",
"teleport",
".",
"Roles",
"{",
"teleport",
".",
"RoleNode",
"}",
",",
"0",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"// create a *ssh.Certificate",
"privateKey",
",",
"err",
":=",
"ssh",
".",
"ParsePrivateKey",
"(",
"privBytes",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"publicKey",
",",
"_",
",",
"_",
",",
"_",
",",
"err",
":=",
"ssh",
".",
"ParseAuthorizedKey",
"(",
"certBytes",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"cert",
",",
"ok",
":=",
"publicKey",
".",
"(",
"*",
"ssh",
".",
"Certificate",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"trace",
".",
"BadParameter",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"// return a ssh.Signer",
"s",
",",
"err",
":=",
"ssh",
".",
"NewCertSigner",
"(",
"cert",
",",
"privateKey",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"return",
"s",
",",
"nil",
"\n",
"}"
] | // generateHostCert will generate a SSH host certificate for a given
// principal. | [
"generateHostCert",
"will",
"generate",
"a",
"SSH",
"host",
"certificate",
"for",
"a",
"given",
"principal",
"."
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/reversetunnel/cache.go#L123-L173 | train |
gravitational/teleport | lib/srv/monitor.go | NewMonitor | func NewMonitor(cfg MonitorConfig) (*Monitor, error) {
if err := cfg.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return &Monitor{
MonitorConfig: cfg,
}, nil
} | go | func NewMonitor(cfg MonitorConfig) (*Monitor, error) {
if err := cfg.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return &Monitor{
MonitorConfig: cfg,
}, nil
} | [
"func",
"NewMonitor",
"(",
"cfg",
"MonitorConfig",
")",
"(",
"*",
"Monitor",
",",
"error",
")",
"{",
"if",
"err",
":=",
"cfg",
".",
"CheckAndSetDefaults",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"trace",
".",
"Wrap",
"(",
"err",
")",
"\n",
"}",
"\n",
"return",
"&",
"Monitor",
"{",
"MonitorConfig",
":",
"cfg",
",",
"}",
",",
"nil",
"\n",
"}"
] | // NewMonitor returns a new monitor | [
"NewMonitor",
"returns",
"a",
"new",
"monitor"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/lib/srv/monitor.go#L107-L114 | train |
gravitational/teleport | tool/teleport/common/teleport.go | OnStart | func OnStart(config *service.Config) error {
return service.Run(context.TODO(), *config, nil)
} | go | func OnStart(config *service.Config) error {
return service.Run(context.TODO(), *config, nil)
} | [
"func",
"OnStart",
"(",
"config",
"*",
"service",
".",
"Config",
")",
"error",
"{",
"return",
"service",
".",
"Run",
"(",
"context",
".",
"TODO",
"(",
")",
",",
"*",
"config",
",",
"nil",
")",
"\n",
"}"
] | // OnStart is the handler for "start" CLI command | [
"OnStart",
"is",
"the",
"handler",
"for",
"start",
"CLI",
"command"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/tool/teleport/common/teleport.go#L169-L171 | train |
gravitational/teleport | tool/teleport/common/teleport.go | onStatus | func onStatus() error {
sshClient := os.Getenv("SSH_CLIENT")
systemUser := os.Getenv("USER")
teleportUser := os.Getenv(teleport.SSHTeleportUser)
proxyHost := os.Getenv(teleport.SSHSessionWebproxyAddr)
clusterName := os.Getenv(teleport.SSHTeleportClusterName)
hostUUID := os.Getenv(teleport.SSHTeleportHostUUID)
sid := os.Getenv(teleport.SSHSessionID)
if sid == "" || proxyHost == "" {
fmt.Println("You are not inside of a Teleport SSH session")
return nil
}
fmt.Printf("User ID : %s, logged in as %s from %s\n", teleportUser, systemUser, sshClient)
fmt.Printf("Cluster Name: %s\n", clusterName)
fmt.Printf("Host UUID : %s\n", hostUUID)
fmt.Printf("Session ID : %s\n", sid)
fmt.Printf("Session URL : https://%s/web/cluster/%v/node/%v/%v/%v\n", proxyHost, clusterName, hostUUID, systemUser, sid)
return nil
} | go | func onStatus() error {
sshClient := os.Getenv("SSH_CLIENT")
systemUser := os.Getenv("USER")
teleportUser := os.Getenv(teleport.SSHTeleportUser)
proxyHost := os.Getenv(teleport.SSHSessionWebproxyAddr)
clusterName := os.Getenv(teleport.SSHTeleportClusterName)
hostUUID := os.Getenv(teleport.SSHTeleportHostUUID)
sid := os.Getenv(teleport.SSHSessionID)
if sid == "" || proxyHost == "" {
fmt.Println("You are not inside of a Teleport SSH session")
return nil
}
fmt.Printf("User ID : %s, logged in as %s from %s\n", teleportUser, systemUser, sshClient)
fmt.Printf("Cluster Name: %s\n", clusterName)
fmt.Printf("Host UUID : %s\n", hostUUID)
fmt.Printf("Session ID : %s\n", sid)
fmt.Printf("Session URL : https://%s/web/cluster/%v/node/%v/%v/%v\n", proxyHost, clusterName, hostUUID, systemUser, sid)
return nil
} | [
"func",
"onStatus",
"(",
")",
"error",
"{",
"sshClient",
":=",
"os",
".",
"Getenv",
"(",
"\"",
"\"",
")",
"\n",
"systemUser",
":=",
"os",
".",
"Getenv",
"(",
"\"",
"\"",
")",
"\n",
"teleportUser",
":=",
"os",
".",
"Getenv",
"(",
"teleport",
".",
"SSHTeleportUser",
")",
"\n",
"proxyHost",
":=",
"os",
".",
"Getenv",
"(",
"teleport",
".",
"SSHSessionWebproxyAddr",
")",
"\n",
"clusterName",
":=",
"os",
".",
"Getenv",
"(",
"teleport",
".",
"SSHTeleportClusterName",
")",
"\n",
"hostUUID",
":=",
"os",
".",
"Getenv",
"(",
"teleport",
".",
"SSHTeleportHostUUID",
")",
"\n",
"sid",
":=",
"os",
".",
"Getenv",
"(",
"teleport",
".",
"SSHSessionID",
")",
"\n\n",
"if",
"sid",
"==",
"\"",
"\"",
"||",
"proxyHost",
"==",
"\"",
"\"",
"{",
"fmt",
".",
"Println",
"(",
"\"",
"\"",
")",
"\n",
"return",
"nil",
"\n",
"}",
"\n\n",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"teleportUser",
",",
"systemUser",
",",
"sshClient",
")",
"\n",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"clusterName",
")",
"\n",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"hostUUID",
")",
"\n",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"sid",
")",
"\n",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"proxyHost",
",",
"clusterName",
",",
"hostUUID",
",",
"systemUser",
",",
"sid",
")",
"\n\n",
"return",
"nil",
"\n",
"}"
] | // onStatus is the handler for "status" CLI command | [
"onStatus",
"is",
"the",
"handler",
"for",
"status",
"CLI",
"command"
] | d5243dbe8d36bba44bf640c08f1c49185ed2c8a4 | https://github.com/gravitational/teleport/blob/d5243dbe8d36bba44bf640c08f1c49185ed2c8a4/tool/teleport/common/teleport.go#L174-L195 | train |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.