mylogin

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2023 License: Apache-2.0 Imports: 11 Imported by: 1

README

mylogin - Go utilities for reading MySQL credentials from ~/.mylogin.cnf

GoDoc Travis-CI Go Report Card

About mylogin.cnf:

Go package

github.com/dolmen-go/mylogin Library for reading and writing ~/.mylogin.cnf.

Utilities

mylogin: dump ~/.mylogin.cnf content in clear
go get -u github.com/dolmen-go/cmd/mylogin
mylogin -h
mylogin-dsn: dump a mylogin.cnf section as a go-sql-driver/mysql connection string prefix
go get -u github.com/dolmen-go/cmd/mylogin-dsn
mylogin-dsn -h

See also

Package github.com/dolmen-go/mylogin-driver/register wraps database/sql driver github.com/go-sql-driver/mysql with an alternate connection string syntax that allows to refers to a ~/.mylogin.cnf section.

License

Copyright 2016-2018 Olivier Mengué

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Overview

Package mylogin reads and writes ~/.mylogin.cnf created by mysql_config_editor.

Reference documentation:

Example:

mysql_config_editor set --login-path=foo --user=bar -p

For usage examples, see the utilies in the same repo.

Index

Constants

View Source
const DefaultSection = "client"

DefaultSection is the name of the base section used by all MySQL client tools.

Variables

This section is empty.

Functions

func DefaultFile

func DefaultFile() string

DefaultFile returns the path to the default mylogin.cnf file:

Windows: %APPDATA%/MySQL/.mylogin.cnf
others: ~/.mylogin.cnf

If the environment variable MYSQL_TEST_LOGIN_FILE is set that path is returned instead.

func Encode

func Encode(w io.Writer, f File) (err error)

Encode writes a mylogin.cnf content encrypted

func FilterSection

func FilterSection(rd io.Reader, section string) io.Reader

FilterSection reads an INI-style content and filter out any section except the given one

Types

type File

type File interface {
	// The key used for encrypting the file
	Key() Key
	// Byte ordering for saving int32 chunk sizes
	ByteOrder() binary.ByteOrder
	// The plaintext content of the file
	PlainText() io.Reader
}

File is the full structure of a mylogin.cnf file.

func Decode

func Decode(input io.Reader) (File, error)

Decode is a filter that returns the plaintext content of a mylogin.cnf file. The file is encrypted with AES 128 CBC with the key embedded in the file.

type Key

type Key [20]byte

Key is a key used for encryption of mylogin.cnf files.

func NewKey

func NewKey(readRandom func([]byte) (int, error)) (Key, error)

NewKey creates a new key from a source of random bytes. See math/rand.Read and crypto/rand.Read as possible sources.

The generated key has the 3 high bits cleared so each byte is non-printable.

func (Key) IsZero

func (k Key) IsZero() bool

type Login

type Login struct {
	User     *string `json:"user,omitempty"`
	Password *string `json:"password,omitempty"`
	Host     *string `json:"host,omitempty"`   // TCP hostname
	Port     *string `json:"port,omitempty"`   // TCP port
	Socket   *string `json:"socket,omitempty"` // Unix socket path
}

Login is the content of a section of mylogin.cnf.

func ReadLogin

func ReadLogin(filename string, sectionNames []string) (login *Login, err error)

ReadLogin reads a mylogin.cnf file, extracts the requested sections and merges them to obtain a single Login (that may be empty).

func (*Login) DSN

func (l *Login) DSN() string

DSN builds a DSN for github.com/go-sql-driver/mysql

The DSN returned always has a '/' at the end. The DSN for an empty Login is just "/".

func (*Login) IsEmpty

func (l *Login) IsEmpty() bool

IsEmpty is true if l is nil or none of the fields are set.

func (*Login) Merge

func (l *Login) Merge(other *Login)

Merge merges l into login: options set in l take precedence over options set in login.

func (*Login) String

func (l *Login) String() string

String returns DSN().

type Section

type Section struct {
	Name  string `json:"name"`
	Login Login  `json:"login"`
}

Section represents one section of the plaintext content of mylogin.cnf.

type Sections

type Sections []Section

Sections represents the structured content on the plaintext of mylogin.cnf.

func Parse

func Parse(rd io.Reader) (sections Sections, err error)

Parse parses the plaintext content of a mylogin.cnf file and returns the structured content.

func ReadSections

func ReadSections(filename string) (sections Sections, err error)

ReadSections reads all Sections of a mylogin.cnf file.

func (Sections) Login

func (sections Sections) Login(section string) *Login

Login returns the Login from the section with the given name.

func (Sections) Merge

func (sections Sections) Merge(sectionNames []string) (login *Login)

Merge returns a single Login which is the result of the ordered merge of the section with the given names (see Login.Merge). For each option the last section that has a value has the precedence.

Directories

Path Synopsis
cmd
mylogin
Command mylogin allows to dump the content of ~/.my.cnf.
Command mylogin allows to dump the content of ~/.my.cnf.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL